您的位置:首页 > 编程语言 > Java开发

spring常用注解使用解析

2017-05-02 18:03 766 查看

spring常用注解使用解析

spring没有采用约定优于配置的策略,spring要求显示指定搜索哪些路径下的Java文件。spring将会把合适的java类全部注册成springBean。

问题:spring怎么知道把哪些Java类当初bean类处理?
这就需要使用annotation,spring使用一些特殊的annotation来标注bean类。

@Component:标准一个普通的springBean类。
@Controller:标注一个控制器组件类。
@Service:标注一个业务逻辑组件类。
@Repository:标注一个DAO组件类。

Bean实例的名称默认是Bean类的首字母小写,其他部分不变。

在spring未来的版本中,@Controller,@Service,@Repository会携带更多语义。尽量考虑使用@Controller,@Service,@Repository代替通用的@Component。

指定了某些类可作为SpringBean类使用后,最好还需要让spring搜索指定路径,此时需要在spring配置文件中导入contextSchema,并指定一个简单的搜索路径。

<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd"><!--自动扫描指定包及其子包下的所有Bean类-->
<context:component-scan
base-package="org.crazyit.app.service"/>
</beans>


我们可以通过为<context:component-scan>添加<include-filter...>或<exclude-filter...>子元素来指定springbean类,只要位于指定路径下的java类满足这种规则,即使这些java类没有使用任何annotation标注,spring一样会将他们当初bean类来处理。

<include-filter...>:满足该规则的java类会被当初bean类处理。

<exclude-filter...>:指定满足该规则的java类不会被当初bean类处理。

这两个子元素有两个属性:
type:指定过滤器类型。
expression:指定过滤器所需要的表达式。

spring内建支持如下四种过滤器:
annotation:该过滤器要指定一个annotation名,如lee.AnnotationTest。
assignable:类名过滤器,该过滤器直接指定一个java类。
regex:正则表达式过滤器,该过滤器指定一个正则表达式,匹配该正则表达式的java类将满足该过滤规则,如org\.example\.default.*。
aspectj:如org.example..*service+。

<!--自动扫描指定包及其子包下的所有Bean类-->
<context:component-scan
base-package="org.crazyit.app.service">
<!--只将以Chinese、Axe结尾的类当成Spring容器中的Bean-->
<context:include-filtertype="regex"
expression=".*Chinese"/>
<context:include-filtertype="regex"
expression=".*Axe"/>
</context:component-scan>


@Resource位于java.annotation包下,来自于javaEE规范的一个annotation。使用该annotation为目标bean指定协作者Bean。
@Resource详细用法见经典javaEE企业应用实战。

@Resource有一个name属性,在默认情况下,spring将这个值解释为需要被注入的Bean实例的名字。



@Controller
publicclassdemo{
@Resource(name="user")
privateUseruser;
@Resource(name="user")
publicvoidsetUser(Useruser){
this.user=user;
}
publicUsergetUser(){
returnuser;
}
}


@Resource也可以直接修饰Filed,

如果@Resource修饰Field,这时候连该属性的setter方法就不需要了。

使用@Resource可以省略name属性。

修饰方法时,省略name属性,则该name值是该setter方法去掉前面的set字符串,首字母小写后得到的子串。
修饰Field时,省略name属性,则该name与该Field同名。

指定Bean实例的作用域。
@Scope:注解也可以指定Bean实例的作用域。

@Controller("demo")
@Scope("prototype")
publicclassdemo{

}


作用范围就那4中填写,不知道的等我博客,最近在回顾spring知识。

@PostConstruct和@PreDestory位于java.annotation包下。
在spring中用于定制spring容器中bean的生命周期行为。
@PostConstruct修饰的方法是bean的初始化之前的方法。

@PreDestory修饰的方法是bean销毁之前的方法。

深刻理解该类使用了@PostConstruct修饰init方法,那么spring就会在该bean的依赖关系注入完成之后回调该方法。

@Component
publicclassSteelAxe
{
publicSteelAxe()
{
System.out.println("创建SteelAxe类对象实例...");
}
}


demo1:

@Component
publicclassChinese
{
//执行Field注入
@Resource(name="steelAxe")
privateSteelAxesteeAxe;

publicChinese(){
super();
System.out.println("创建Chinese类对象实例...");
}
@PostConstruct
publicvoidinit()
{
System.out.println("正在执行初始化的init方法...");
}
@PreDestroy
publicvoidclose()
{
System.out.println("正在执行销毁之前的close方法...");
}
}



//创建Spring容器
AbstractApplicationContextctx=new
ClassPathXmlApplicationContext("beans.xml");
//注册关闭钩子
ctx.registerShutdownHook();


打印:

创建Chinese类对象实例...

创建SteelAxe类对象实例...

正在执行初始化的init方法...

正在执行销毁之前的close方法...


如果注释掉chinese的依赖注入,那么结果如下:

@Component
publicclassChinese
{
//执行Field注入
//@Resource(name="steelAxe")
//privateSteelAxesteeAxe;
publicChinese(){
super();
System.out.println("创建Chinese类对象实例...");
}
@PostConstruct
publicvoidinit()
{
System.out.println("正在执行初始化的init方法...");
}
@PreDestroy
publicvoidclose()
{
System.out.println("正在执行销毁之前的close方法...");
}
}


打印:

创建Chinese类对象实例...

正在执行初始化的init方法...

创建SteelAxe类对象实例...

正在执行销毁之前的close方法...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐