Spring注入
Spring注入是指在启动Spring容器加载bean配置的时候,完成对变量的赋值行为。
常用的两种注入方式:
- setter注入
- 构造注入
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <!-- setter注入 --> <bean id="setterService" class="com.injavawetrust.ioc.injection.service.SetterServiceImpl"> <property name="setterDao" ref="setterDao"></property> </bean> <!-- 构造注入 --> <bean id="constructorService" class="com.injavawetrust.ioc.injection.service.ConstructorServiceImpl"> <constructor-arg name="constructorDao" ref="constructorDao"></constructor-arg> </bean> <bean id="setterDao" class="com.injavawetrust.ioc.injection.dao.SetterDaoImpl"></bean> <bean id="constructorDao" class="com.injavawetrust.ioc.injection.dao.ConstructorDaoImpl"></bean> </beans>
import junit.framework.TestCase; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import org.springframework.beans.BeansException; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.injavawetrust.ioc.injection.service.ConstructorService; import com.injavawetrust.ioc.injection.service.SetterService; /** * @author InJavaWeTrust */ @RunWith(BlockJUnit4ClassRunner.class) public class TestInjection extends TestCase { private ClassPathXmlApplicationContext context; @Before public void before() { try { context = new ClassPathXmlApplicationContext("classpath:spring-injavawetrust.xml"); context.start(); } catch (BeansException e) { e.printStackTrace(); } } @After public void after() { context.destroy(); } @Test public void tesSetter() { SetterService service = (SetterService) context.getBean("setterService"); service.say("setter-injavawetrust"); } @Test public void testConstructor() { ConstructorService service = (ConstructorService) context.getBean("constructorService"); service.say("constructor-injavawetrust"); } }
/** * @author InJavaWeTrust */ public interface SetterService { public void say(String str); }
import com.injavawetrust.ioc.injection.dao.SetterDao; /** * @author InJavaWeTrust */ public class SetterServiceImpl implements SetterService { private SetterDao setterDao; public void setSetterDao(SetterDao setterDao) { this.setterDao = setterDao; } @Override public void say(String str) { System.out.println("接受参数:" + str); str = "hello " + str; setterDao.say(str); } }
/** * @author InJavaWeTrust */ public interface SetterDao { public void say(String str); }
/** * @author InJavaWeTrust */ public class SetterDaoImpl implements SetterDao { @Override public void say(String str) { System.out.println("say: " + str); } }
/** * @author InJavaWeTrust */ public interface ConstructorService { public void say(String str); }
import com.injavawetrust.ioc.injection.dao.ConstructorDao; /** * @author InJavaWeTrust */ public class ConstructorServiceImpl implements ConstructorService { private ConstructorDao constructorDao; public ConstructorServiceImpl(ConstructorDao constructorDao) { this.constructorDao = constructorDao; } @Override public void say(String str) { System.out.println("接受参数:" + str); str = "hello " + str; constructorDao.say(str); } }
/** * @author InJavaWeTrust */ public interface ConstructorDao { public void say(String str); }
/** * @author InJavaWeTrust */ public class ConstructorDaoImpl implements ConstructorDao { @Override public void say(String str) { System.out.println(str); } }