Set注入,基于set方法实现的,底层通过反射机制调用属性对应的set方法然后给属性赋值。这种方式要求属性必须对外提供set方法。
#pom.xml配置 <!-- 打包方式jar--> <packaging>jar</packaging> <!--配置多个仓库 --> <repositories> <!-- Spring6 --> <repository> <id>repository.spring.milestone</id> <name>Spring Milestone Repository</name> <url>https://repo.spring.io/milestone</url> </repository> </repositories> <!-- 依赖--> <dependencies> <!-- log4j2的依赖--> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.19.0</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j2-impl</artifactId> <version>2.19.0</version> </dependency> <!-- Spring context依赖 当引入Spring context依赖后,表示将Spring的基础依赖引入 如果想使用spring的jdbc,或其他的tx,那么还需要再次添加依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.0.0-M2</version> </dependency> <!--junit依赖--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies>
#log4j2.xml配置 <?xml version="1.0" encoding="UTF-8"?> <configuration> <loggers> <!-- level指定日志级别 从低到高 ALL<TRACE<DEBUG<INFO<WARN<ERROR<FATAL<OFF --> <root level="DEBUG"> <!-- DEBUG意思是错误信息从DEBUG开始往右 --> <appender-ref ref="spring6log"/> </root> </loggers> <appenders> <!-- 输出日志信息到控制台--> <console name="spring6log" target="SYSTEM_OUT"> <!-- --> <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%t] %-3level %logger{1024} - %msg%n"/> </console> </appenders> </configuration>
#Dao层 import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class testDao { private static final Logger logger = LoggerFactory.getLogger(testDao.class);//记录SpringTest.class类的日记 public void insert(){ logger.info("testDao----"); } } import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class UserDao { private static final Logger logger = LoggerFactory.getLogger(UserDao.class);//记录SpringTest.class类的日记 public void insert(){ logger.info("insert正在被调用----"); } }
#Service层 public class UserService { private UserDao userDao; private testDao testDao; //IDEA生成的set方法符合javabean规范 public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void setTestDao(com.spring6.dao.testDao testDao) { this.testDao = testDao; } public void saveUser(){ //保存用户信息到数据库 //set注入的话,必须提供一个set方法. Spring容器会调用这个set方法,来给userDao属性赋值。 userDao.insert(); testDao.insert(); } }
#Springconfig.xml文件配置 <!-- id:唯一, calss:类的全限定名称 --> <!-- 配置dao--> <bean id="UserDaobean" class="com.spring6.dao.UserDao"></bean> <bean id="testDaobean" class="com.spring6.dao.testDao"></bean> <!-- 配置server--> <bean id="UserServicebean" class="com.spring6.service.UserService"> <!--创建UserServicebean对象--> <!-- Spring调用对应的set方法,需配置property标签--> <!-- name属性怎么指定值:set方法的方法名,去掉set,把首字母改为小写--> <!-- ref引用,ref指定的是哟啊注入的bean的id--> <property name="userDao" ref="UserDaobean"></property> <property name="testDao" ref="testDaobean"></property> </bean>
#测试类 public class Springtest { @Test public void testSet(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Springconfig.xml"); UserService userService =applicationContext.getBean("UserServicebean", UserService.class); userService.saveUser(); } }