Spring 1 IoC 4 注解
2015-06-04 00:00
344 查看
如果采用注解方式,下面代码
<!-- Inform Spring to scan the annotation within codes for DI requirements -->
<context:annotation-config/>
<!-- Inform Spring to scan the injectable components base on packages -->
<
context:component-scan
base-package
=
"com.apress.prospring3.ch4.annotation;com.apress.prospring3.ch4.annotation1"
>
<
context:exclude-filter
type
=
"assignable"
expression
=
"com.apress.prospring3.ch4.annotation1.Bean2"
/>
</
context:component-scan
>
自动扫描有下面注解的类:
@Component,
@Controller ,@Repository,
@Service
@Service ("messageRenderer" )
public
class
StandardOutMessageRenderer
implements
MessageRenderer {
@Autowired
在构造方法上添加
@Autowired,只能在一个构造方法上,然后要创建一个bean,用作构造方法的参数,下面用index方式创建
<bean id = "message" class="java.lang.String"
c:_0="this is a c:_0"/>
在Setter方法上
@Autowired
//@Resource(name="messageProvider") // Effect is the same as Autowired
public
void
setMessageProvider(MessageProvider provider) {
this
.
messageProvider
= provider;
}
@Value,注入SpEL表达式
@Value("${app.ctx}")
private String _ctxPath;
@PostConstruct和PreDestroy:通过注解指定初始化和销毁方法定义
<!-- Inform Spring to scan the annotation within codes for DI requirements -->
<context:annotation-config/>
<!-- Inform Spring to scan the injectable components base on packages -->
<
context:component-scan
base-package
=
"com.apress.prospring3.ch4.annotation;com.apress.prospring3.ch4.annotation1"
>
<
context:exclude-filter
type
=
"assignable"
expression
=
"com.apress.prospring3.ch4.annotation1.Bean2"
/>
</
context:component-scan
>
自动扫描有下面注解的类:
@Component,
@Controller ,@Repository,
@Service
@Service ("messageRenderer" )
public
class
StandardOutMessageRenderer
implements
MessageRenderer {
@Autowired
在构造方法上添加
@Autowired,只能在一个构造方法上,然后要创建一个bean,用作构造方法的参数,下面用index方式创建
<bean id = "message" class="java.lang.String"
c:_0="this is a c:_0"/>
在Setter方法上
@Autowired
//@Resource(name="messageProvider") // Effect is the same as Autowired
public
void
setMessageProvider(MessageProvider provider) {
this
.
messageProvider
= provider;
}
@Value,注入SpEL表达式
@Value("${app.ctx}")
private String _ctxPath;
@PostConstruct和PreDestroy:通过注解指定初始化和销毁方法定义
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring中属性注入详解
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- java实现简单美女拼图游戏
- java基本教程之线程休眠 java多线程教程
- JSP开发中在spring mvc项目中实现登录账号单浏览器登录
- 基于Spring框架的Shiro配置方法
- Spring MVC中上传文件实例
- 实例讲解Java的Spring框架中的AOP实现
- 解析Java中如何获取Spring中配置的bean
- Spring的注解配置与XML配置之间的比较
- java Spring整合Freemarker的详细步骤
- Java 二维码,QR码,J4L-QRCode 的资料整理
- java当中的定时器的4种使用方式
- java中 spring 定时任务 实现代码
- spring mvc实现登录账号单浏览器登录