1 解析
1.1 @bean注解定义
1.2 @bean注解的使用
2 代码演练
2.1 @bean的应用不带name
2.2 @bean的应用带name
2.3 @bean注解调用initMethod和destroyMethod
1 解析
1.1 @bean注解定义
@bean标识一个用于配置和初始化一个由springIOC容器管理的新对象的方法(生成ioc容器的bean实例),类似于xml配置的</bean>
1.2 @bean注解和@Configuration的使用
a 通常注解@bean和@Configuration使用
b @bean(name="",initMethod="",destroyMethod="")(详细使用见代码演练部分)
c @Configuration 该类创建各种Bean交付IoC容器(Bean类代码为纯净JavaBean)
2 代码演练
2.1 @bean的应用不带name
实体类:
package com.imooc.beanannotation.javabased; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class StoreConfig { @Bean public Store getStringStore(){ return new StringStore(); } }
测试类:
package com.imooc.test.beanannotation; import org.junit.Test; import com.imooc.test.base.UnitTestBase; public class TestJavaBased extends UnitTestBase{ public TestJavaBased(){ super("classpath*:spring-beanannotation.xml"); } @Test public void testStoreConfig(){ System.out.println(super.getbean("getStringStore").getClass().getName()); } }
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"/> </beans>
impl类:
package com.imooc.beanannotation.javabased; public class StringStore implements Store { }
dao类:
package com.imooc.beanannotation.javabased; public interface Store { }
打印结果:
三月 25, 2019 6:44:28 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5a62a404: startup date [Mon Mar 25 06:44:28 CST 2019]; root of context hierarchy 三月 25, 2019 6:44:28 上午 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] com.imooc.beanannotation.javabased.StringStore 三月 25, 2019 6:44:29 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@5a62a404: startup date [Mon Mar 25 06:44:28 CST 2019]; root of context hierarchy
2.2 @bean的应用带name
实体类:
package com.imooc.beanannotation.javabased; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class StoreConfig { @Bean(name="store") public Store getStringStore(){ return new StringStore(); } }
测试类:
package com.imooc.test.beanannotation; import org.junit.Test; import com.imooc.test.base.UnitTestBase; public class TestJavaBased extends UnitTestBase{ public TestJavaBased(){ super("classpath*:spring-beanannotation.xml"); } @Test public void testStoreConfig(){ System.out.println(super.getbean("store").getClass().getName()); } }
打印结果:
三月 25, 2019 6:57:00 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2121052: startup date [Mon Mar 25 06:57:00 CST 2019]; root of context hierarchy 三月 25, 2019 6:57:00 上午 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] com.imooc.beanannotation.javabased.StringStore 三月 25, 2019 6:57:01 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@2121052: startup date [Mon Mar 25 06:57:00 CST 2019]; root of context hierarchy
2.3 bean注解调用initMethod和destroyMethod
实体类:
package com.imooc.beanannotation.javabased; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class StoreConfig { @Bean(name="store",initMethod="init",destroyMethod="destroy") public Store getStringStore(){ return new StringStore(); } }
impl类:
package com.imooc.beanannotation.javabased; public class StringStore implements Store { public void init(){ System.out.println("This is init Method"); } public void destroy(){ System.out.println("This is destroy Method"); } }
打印结果:
三月 26, 2019 6:15:23 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5fa34e31: startup date [Tue Mar 26 06:15:23 CST 2019]; root of context hierarchy 三月 26, 2019 6:15:23 上午 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] This is init Method com.imooc.beanannotation.javabased.StringStore 三月 26, 2019 6:15:24 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@5fa34e31: startup date [Tue Mar 26 06:15:23 CST 2019]; root of context hierarchy This is destroy Method