IoCといくつかの基本的な

4177 ワード

1 CostDao.JAva永続化層インタフェース
package dao;

public interface CostDao {
	public void save();
	public void delete();
}

2つの実装クラスJdbcCostDao.java  &  HibernateCostDao.java
package dao;

public class JdbcCostDao implements CostDao {
	
	public JdbcCostDao() {
		System.out.println(" JdbcCostDao ");
	}

	@Override
	public void save() {
		System.out.println("Jdbc save()");
	}

	@Override
	public void delete() {
		System.out.println("Jdbc delete()");
	}
}
package dao;

public class HibernateCostDao implements CostDao {
	
	public void myinit() {
		System.out.println("hibernate myinit()");
	}
	
	public HibernateCostDao() {
		System.out.println(" HibernateCostDao ");
	}

	@Override
	public void save() {
		System.out.println("hibernate save()");
	}

	@Override
	public void delete() {
		System.out.println("hibernate delete()");
	}
}

3 DeleteAction.java
package action;

import dao.CostDao;

public class DeleteAction {
	private CostDao dao;
	//  name="dao"  dao, 
	public void setDao(CostDao dao) {
		this.dao = dao;
	}
	
	public String execute() {
		System.out.println(" ");
		dao.delete();
		return "success";
	}
}

4ガイドパック
5 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:tx="http://www.springframework.org/schema/tx" 
		xmlns:aop="http://www.springframework.org/schema/aop"
		xmlns:context="http://www.springframework.org/schema/context" 
		xmlns:jee="http://www.springframework.org/schema/jee"
		xsi:schemaLocation="
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
			http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
			http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
	<!--	
		prototype 	 ( ),Bean getBean 
	 	singleton 	 ,Bean 
		lazy-init 	Bean getBean 
		init-method 	 , 。
		destroy-method	 , Bean , mydesctory 。( singleton Bean )
	-->
	<bean id="hibernateCostDao" scope="singleton" lazy-init="true" init-method="myinit" class="dao.HibernateCostDao"></bean>
	
	<bean id="jdbcCostDao" class="dao.JdbcCostDao"></bean>
	
	<bean id="deleteAction" scope="prototype" class="action.DeleteAction">
		<!-- ref bean -->
		<property name="dao" ref="hibernateCostDao"></property>
	</bean>
</beans>

6テスト
package test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dao.CostDao;

public class TestBean {
	@Test
	public void testCreate() {
		String conf = "applicationContext.xml";
		ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
		CostDao dao1 = (CostDao) ac.getBean("hibernateCostDao");
		CostDao dao2 = (CostDao) ac.getBean("hibernateCostDao");
		//  
		System.out.println(dao1 == dao2);
	}
}
package test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import action.DeleteAction;

public class TestAction {
	@Test
	public void testDelete() {
		String conf = "applicationContext.xml";
		ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
		DeleteAction action = (DeleteAction) ac.getBean("deleteAction");
		action.execute();
	}
}