Spring学习三----------注入方式
© 版权声明:本文为博主原创文章,转载请注明出处
Spring注入方式
本篇博客只讲最常用的两种注入方式:设值注入和构造器注入。代码为完整代码,复制即可使用。
1.目录结构
2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.spring</groupId> <artifactId>Spring</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Spring</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>4.3.7.RELEASE</spring.version> </properties> <dependencies> <!-- junit依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- spring依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> </dependencies> </project>
2.spring-ioc.xml(两种注入方式,任选一种)
2.1 设值注入
<?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"> <bean id="injectService" class="org.spring.ioc.injection.service.impl.InjectionServiceImpl"> <!-- 设值注入 --> <property name="injectDao" ref="injectDao"/> </bean> <bean id="injectDao" class="org.spring.ioc.injection.dao.impl.InjectionDaoImpl"></bean> </beans>
2.2 构造器注入
<?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"> <bean id="injectService" class="org.spring.ioc.injection.service.impl.InjectionServiceImpl"> <!-- 构造器注入 --> <constructor-arg name="injectDao" ref="injectDao"/> </bean> <bean id="injectDao" class="org.spring.ioc.injection.dao.impl.InjectionDaoImpl"></bean> </beans>
3.InjectionDao
package org.spring.ioc.injection.dao; public interface InjectionDao { String save(String word); }
4.InjectionDaoImpl
package org.spring.ioc.injection.dao.impl; import org.spring.ioc.injection.dao.InjectionDao; public class InjectionDaoImpl implements InjectionDao { public String save(String word) { return "数据保存成功!保存的数据是:" + word.hashCode(); } }
5.InjectionService
package org.spring.ioc.injection.service; public interface InjectionService { String save(String word); }
6.InjectionServiceImpl(两种注入方式,任选一种)
6.1 设值注入
package org.spring.ioc.injection.service.impl; import org.spring.ioc.injection.dao.InjectionDao; import org.spring.ioc.injection.service.InjectionService; public class InjectionServiceImpl implements InjectionService { private InjectionDao injectDao; //设值注入 public void setInjectDao(InjectionDao injectDao) { this.injectDao = injectDao; } public String save(String word) { System.out.println("传递过来的参数是:" + word); return injectDao.save(word); } }
6.2 构造器注入
package org.spring.ioc.injection.service.impl; import org.spring.ioc.injection.dao.InjectionDao; import org.spring.ioc.injection.service.InjectionService; public class InjectionServiceImpl implements InjectionService { private InjectionDao injectDao; //构造器注入 public InjectionServiceImpl(InjectionDao injectDao) { this.injectDao = injectDao; } public String save(String word) { System.out.println("传递过来的参数是:" + word); return injectDao.save(word); } }
7.UnitTestBase.java
package org.spring.ioc.test; import org.junit.After; import org.junit.Before; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.util.StringUtils; /** * 单元测试初始化类 * */ public class UnitTestBase { private ClassPathXmlApplicationContext context; private String springXmlPath; /** * 无参构造器 */ public UnitTestBase() { } /** * 含参构造器 * * @param springXmlPath * spring配置文件路径 */ public UnitTestBase(String springXmlPath) { this.springXmlPath = springXmlPath; } /** * 初始化spring配置文件 */ @Before//在@Test注解的方法执行前执行 public void before() { if(StringUtils.isEmpty(springXmlPath)) {//默认spring配置文件路径 springXmlPath = "classpath*:spring-*.xml"; } //加载配置文件 context = new ClassPathXmlApplicationContext(springXmlPath.split("[,\\s]+")); //启动组件 context.start(); } /** * 销毁spring组件 */ @After//在@Test注解的方法执行后执行 public void after() { context.destroy();//销毁组件 } /** * 获取spring中定义的bean实例 * * @param beanId * * @return */ @SuppressWarnings("unchecked") protected <T extends Object> T getBean(String beanId) { return (T) context.getBean(beanId); } /** * 获取spring中定义的bean实例 * * @param clazz * * @return */ protected <T extends Object> T getBean(Class<T> clazz) { return (T) context.getBean(clazz); } }
8.TestInject.java
package org.spring.ioc.test; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import org.spring.ioc.injection.service.InjectionService; @RunWith(BlockJUnit4ClassRunner.class)//指定JUnit默认执行类 public class TestInject extends UnitTestBase { public TestInject() {//通过构造方法传入spring配置文件路径 super("classpath*:spring-ioc.xml"); } @Test public void testInj() { InjectionService injectService = super.getBean("injectService");//从spring容器中获取依赖对象 System.out.println(injectService.save("Word.")); } }
9.效果预览
© 版权声明:本文为博主原创文章,转载请注明出处