1 解析
2 代码演练
1 解析
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