1 解析
1.1 疑问:2.2去掉@resource注解,为什么不能赋值?不是有set方法了吗?
2 代码演练
2.1 给变量赋值(方法一)
2.2 给变量赋值(方法二)
2.3 注解PostConstruct 和注解 PreDestroy(前置注解和后置注解)
1 解析
1.1 疑问:2.2去掉@resource注解,为什么不能赋值?不是有set方法了吗?
2 代码演练
2.1 给变量赋值
实体类:
package com.imooc.beanannotation.javabased; import javax.annotation.Resource; import org.springframework.stereotype.Service; @Service public class JsrService { @Resource private JsrDao jsrDao; // public void setJsrDao(JsrDao jsrDao) { // this.jsrDao = jsrDao; // } public void save(){ jsrDao.save(); } }
测试类:
package com.imooc.test.beanannotation; import org.junit.Test; import com.imooc.beanannotation.javabased.JsrService; import com.imooc.test.base.UnitTestBase; public class TestJsr extends UnitTestBase{ public TestJsr(){ super("classpath*:spring-beanannotation.xml"); } @Test public void testJsr(){ JsrService jsrService =super.getbean("jsrService"); jsrService.save(); } }
dao类:
package com.imooc.beanannotation.javabased; import org.springframework.stereotype.Repository; @Repository public class JsrDao { public void save(){ System.out.println("JsrDao is saving!"); } }
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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.imooc.beanannotation"></context:component-scan> </beans>
打印日志:
四月 07, 2019 8:03:26 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org[email protected]5fa34e31: startup date [Sun Apr 07 20:03:26 CST 2019]; root of context hierarchy 四月 07, 2019 8:03:27 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beanannotation.xml] 四月 07, 2019 8:03:27 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [config.xml] JsrDao is saving! 四月 07, 2019 8:03:28 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org[email protected]5fa34e31: startup date [Sun Apr 07 20:03:26 CST 2019]; root of context hierarchy
2.2 给变量赋值(方法二)
实体类:
package com.imooc.beanannotation.javabased; import javax.annotation.Resource; import org.springframework.stereotype.Service; @Service public class JsrService { // @Resource private JsrDao jsrDao; @Resource public void setJsrDao(JsrDao jsrDao) { this.jsrDao = jsrDao; } public void save(){ jsrDao.save(); } }
2.3 注解PostConstruct 和注解 PreDestroy(前置注解和后置注解)
实体类:
package com.imooc.beanannotation.javabased; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; import org.springframework.stereotype.Service; @Service public class JsrService { // @Resource private JsrDao jsrDao; @Resource public void setJsrDao(JsrDao jsrDao) { this.jsrDao = jsrDao; } @PostConstruct public void init(){ System.out.println("init method"); } @PreDestroy public void destroy(){ System.out.println("destroy method"); } public void save(){ jsrDao.save(); } }
打印日志:
四月 07, 2019 8:25:30 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org[email protected]7c27a30c: startup date [Sun Apr 07 20:25:30 CST 2019]; root of context hierarchy 四月 07, 2019 8:25:30 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beanannotation.xml] 四月 07, 2019 8:25:31 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [config.xml] init method JsrDao is saving! 四月 07, 2019 8:25:31 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org[email protected]7c27a30c: startup date [Sun Apr 07 20:25:30 CST 2019]; root of context hierarchy destroy method
原文地址:https://www.cnblogs.com/1446358788-qq/p/10666796.html
时间: 2024-10-29 02:36:40