在之前的文章中,对bean的定义,以及作用域等定义,一般是通过xml文件来实现的。
在jdk中,有一种注解的方法,如之前的@Test,可以省去在xml文件中对bean的配置。
1.常用的java(注解)定义bean的方式
2.为了检测相应的注解bean,xml文件的配置
3.使用过滤器进行扫描
4.过滤器的类型
5.作用域scope
6.bean定义
下面给出对于bean定义,以及scope的例子,可对照之前xml配置方式学习。
1.bean的定义例子(未定义bean name)
//1.类
package com.imooc.beanannotation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
//注解未指定bean的id,默认类名首字母小写
@Component
public class BeanAnnotation {
public void say(String arg) {
System.out.println("BeanAnnotation : " + arg);
}
}
//2.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>
//3.测试类
package com.imooc.test.beanannotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.imooc.beanannotation.BeanAnnotation;
import com.imooc.test.base.UnitTestBase;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanAnnotation extends UnitTestBase {
public TestBeanAnnotation() {
super("classpath*:spring-beanannotation.xml");
}
@Test
public void testSay() {
//注解未指定bean的id,默认为首字母小写
BeanAnnotation bean = super.getBean("beanAnnotation");
bean.say("This is test.");
}
}
结果:
8月 06, 2018 11:12:51 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Mon Aug 06 11:12:51 CST 2018]; root of context hierarchy
8月 06, 2018 11:12:51 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from URL [file:/E:/eclipse/workspace/Spring/classes/spring-beanannotation.xml]
8月 06, 2018 11:12:52 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [config.xml]
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$2 (file:/E:/eclipse/workspace/Spring/lib/spring/spring-core-4.0.5.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$2
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
8月 06, 2018 11:12:52 上午 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
信息: Loading properties file from class path resource [config.properties]
8月 06, 2018 11:12:52 上午 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
BeanAnnotation : This is test.
8月 06, 2018 11:12:52 上午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Mon Aug 06 11:12:51 CST 2018]; root of context hierarchy
2.bean定义例子(自定义bean name)
//1.类
package com.imooc.beanannotation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
//自定义bean的name为bean
@Component("bean")
public class BeanAnnotation {
public void say(String arg) {
System.out.println("BeanAnnotation : " + arg);
}
}
//2.测试类
package com.imooc.test.beanannotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.imooc.beanannotation.BeanAnnotation;
import com.imooc.test.base.UnitTestBase;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanAnnotation extends UnitTestBase {
public TestBeanAnnotation() {
super("classpath*:spring-beanannotation.xml");
}
@Test
public void testSay() {
//这里需要改成指定的bean name来获取相应的bean
BeanAnnotation bean = super.getBean("bean");
bean.say("This is test.");
}
结果:
8月 06, 2018 11:16:42 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Mon Aug 06 11:16:42 CST 2018]; root of context hierarchy
8月 06, 2018 11:16:43 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from URL [file:/E:/eclipse/workspace/Spring/classes/spring-beanannotation.xml]
8月 06, 2018 11:16:43 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [config.xml]
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$2 (file:/E:/eclipse/workspace/Spring/lib/spring/spring-core-4.0.5.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$2
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
8月 06, 2018 11:16:43 上午 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
信息: Loading properties file from class path resource [config.properties]
8月 06, 2018 11:16:43 上午 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
BeanAnnotation : This is test.
8月 06, 2018 11:16:43 上午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Mon Aug 06 11:16:42 CST 2018]; root of context hierarchy
3.bean的scope例子,默认类型
//1.类
package com.imooc.beanannotation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
//不指定scope类型,默认为singleton
@Scope
@Component
public class BeanAnnotation {
public void myHashCode() {
System.out.println("BeanAnnotation : " + this.hashCode());
}
}
//2.测试类
package com.imooc.test.beanannotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.imooc.beanannotation.BeanAnnotation;
import com.imooc.test.base.UnitTestBase;
@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanAnnotation extends UnitTestBase {
public TestBeanAnnotation() {
super("classpath*:spring-beanannotation.xml");
}
@Test
public void testScpoe() {
BeanAnnotation bean = super.getBean("beanAnnotation");
bean.myHashCode();
bean = super.getBean("beanAnnotation");
bean.myHashCode();
}
}
结果:
8月 06, 2018 11:21:02 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Mon Aug 06 11:21:02 CST 2018]; root of context hierarchy
8月 06, 2018 11:21:02 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from URL [file:/E:/eclipse/workspace/Spring/classes/spring-beanannotation.xml]
8月 06, 2018 11:21:02 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [config.xml]
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$2 (file:/E:/eclipse/workspace/Spring/lib/spring/spring-core-4.0.5.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$2
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
8月 06, 2018 11:21:02 上午 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
信息: Loading properties file from class path resource [config.properties]
8月 06, 2018 11:21:02 上午 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
BeanAnnotation : 1908571880
BeanAnnotation : 1908571880
8月 06, 2018 11:21:02 上午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Mon Aug 06 11:21:02 CST 2018]; root of context hierarchy
可见返回的是同一个bean。
4.scope例子,prototype类型
//1.类
package com.imooc.beanannotation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Scope("prototype")
@Component
public class BeanAnnotation {
/*public void say(String arg) {
System.out.println("BeanAnnotation : " + arg);
}*/
public void myHashCode() {
System.out.println("BeanAnnotation : " + this.hashCode());
}
}
结果:
8月 06, 2018 11:24:27 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Mon Aug 06 11:24:27 CST 2018]; root of context hierarchy
8月 06, 2018 11:24:27 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from URL [file:/E:/eclipse/workspace/Spring/classes/spring-beanannotation.xml]
8月 06, 2018 11:24:27 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [config.xml]
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils$2 (file:/E:/eclipse/workspace/Spring/lib/spring/spring-core-4.0.5.RELEASE.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils$2
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
8月 06, 2018 11:24:27 上午 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
信息: Loading properties file from class path resource [config.properties]
8月 06, 2018 11:24:27 上午 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
BeanAnnotation : 1908571880
BeanAnnotation : 1048434276
8月 06, 2018 11:24:27 上午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@146044d7: startup date [Mon Aug 06 11:24:27 CST 2018]; root of context hierarchy
可见,返回的已经是不同的bean了。