Spring IoC — 基于注解的配置
基于XML的配置,Bean定义信息和Bean实现类本身是分离的,而采用基于注解的配置方式时,Bean定义信息即通过在Bean实现类上标注注解实现。
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
>
<context:component-scan base-package="com.ioc.ch4_10"/>
<bean id="ls" class="com.ioc.ch4_10.LogonService"></bean>
<!-- context:component-scan base-package="com.baobaotao" resource-pattern="anno/*.class"/ -->
<!-- context:component-scan base-package="com.baobaotao">
<context:include-filter type="regex" expression="com\.baobaotao\.anno.*Dao"/>
<context:include-filter type="regex" expression="com\.baobaotao\.anno.*Service"/>
<context:exclude-filter type="aspectj" expression="com.baobaotao..*Controller+"/>
</context:component-scan -->
</beans>
LogDao类:
package com.ioc.ch4_10;
import org.springframework.stereotype.Component;
@Component("logDao")
public class LogDao {
}
UserDao_1类:
package com.ioc.ch4_10;
import org.springframework.stereotype.Component;
@Component("userDao_1")
//自动被Spring容器识别,Spring容器自动将POJO转换为容器管理的Bean
//等价于XML配置的<bean id="userDao_1" class="com.ioc.ch4_10.UserDao_1">
public class UserDao_1 {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "UserDao_1{" +
"name='" + name + '\t' +
'}';
}
public UserDao_1() {
System.out.println("userDao");
}
}
UserDao_2类:
package com.ioc.ch4_10;
import org.springframework.stereotype.Component;
/**
* Created by gao on 16-3-25.
*/
@Component("userDao_2")
public class UserDao_2 {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "UserDao_2{" +
"name='" + name + '\t' +
'}';
}
public UserDao_2() {
System.out.println("userDao_2");
}
}
UserDao_3类:
package com.ioc.ch4_10;
import org.springframework.stereotype.Component;
/**
* Created by gao on 16-3-25.
*/
@Component("userDao_3")
public class UserDao_3 {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "UserDao_3{" +
"name='" + name + '\t' +
'}';
}
public UserDao_3() {
System.out.println("userDao_3");
}
}
LogonService类:
package com.ioc.ch4_10;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class LogonService{
//使用@Autowired进行自动注入
@Autowired
private LogDao logDao;
@Autowired
private UserDao_1 userDao1;
//使用@Autowired的required属性,设置为false表示找不到匹配的Bean完成注入也不要抛出异常
@Autowired(required = false)
private UserDao_2 userDao_2;
//使用@Qualifier指定注入Bean的名称
@Autowired
@Qualifier("userDao_3")
private UserDao_3 userDao_3;
public LogDao getLogDao() {
return logDao;
}
public UserDao_1 getUserDao1() {
return userDao1;
}
public UserDao_2 getUserDao2() {
return userDao_2;
}
public UserDao_3 getUserDao3() {
return userDao_3;
}
public void printSomething(){
userDao1.setName("LogonService-userDao1");
System.out.println(userDao1.toString());
userDao_2.setName("LogonService-userDao2");
System.out.println(userDao_2.toString());
userDao_3.setName("LogonService-userDao3");
System.out.println(userDao_3.toString());
}
}
LoginService_2类:
package com.ioc.ch4_10;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
/**
* Created by gao on 16-3-25.
*/
@Service
public class LogonService_2 {
private LogDao logDao;
private UserDao_1 userDao_1;
//自动将LogDao传给方法入参
@Autowired
public void setLogDao(LogDao logDao) {
System.out.println("auto setlogdao");
this.logDao = logDao;
}
//自动将名为userDao的Bean传给方法入参
@Autowired
public void setUserDao_1(UserDao_1 userDao_1) {
System.out.println("auto setuserdao");
this.userDao_1 = userDao_1;
}
@Autowired
public void init(@Qualifier("userDao_1")UserDao_1 userDao,LogDao logDao){
System.out.println("multi param inject");
this.userDao_1 = userDao;
this.logDao =logDao;
}
}
测试类:
package com.ioc.ch4_10;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by gao on 16-3-25.
*/
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com\\ioc\\ch4_10\\beans.xml");
LogonService ls = (LogonService) ctx.getBean("ls");
ls.printSomething();
}
}
package com.ioc.ch4_10;
public interface Plugin {
}
onePlugin类:
package com.ioc.ch4_10;
import org.springframework.stereotype.Component;
@Component
public class OnePlugin implements Plugin {
}
twoPlugin类:
package com.ioc.ch4_10;
import org.springframework.stereotype.Component;
@Component
public class TwoPlugin implements Plugin {
}
MyComponent类:
package com.ioc.ch4_10;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class MyComponent {
//Spring会将容器中所有类型为Plugin的Bean注入到这个变量中
@Autowired(required=false)
private List<Plugin> plugins;
public List<Plugin> getPlugins() {
return plugins;
}
}
mycomponent.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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.ioc.ch4_10">
<context:include-filter type="aspectj" expression="com.baobaotao.anno.*Plugin+"/>
<context:include-filter type="aspectj" expression="com.baobaotao.anno.MyComponent"/>
</context:component-scan>
</beans>
MyComponentTest类:
package com.ioc.ch4_10;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyComponentTest {
public static void main(String[] args) throws Throwable {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com\\ioc\\ch4_10\\mycomponent.xml");
MyComponent myComponent = ctx.getBean(MyComponent.class);
System.out.println("pulgin.size:"+myComponent.getPlugins().size()); }
}
package com.ioc.ch4_10;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
//指定Bean的作用范围为prototype
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Component
public class Car {
private int maxSpeed;
public String brand;
private double price; ......
}
Boss类:
package com.ioc.ch4_10;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
@Component
public class Boss { private Car car;
public Boss(){
System.out.println("construct...");
}
@Resource
private void setCar(Car car){
System.out.println("execute in setCar");
this.car = car;
} @PostConstruct
private void init1(){
System.out.println("execute in init1");
} @PostConstruct
private void init2(){
System.out.println("execute in init1");
} @PreDestroy
private void destory1(){
System.out.println("execute in destory1");
} @PreDestroy
private void destory2(){
System.out.println("execute in destory2");
}
}
测试类:
package com.ioc.ch4_10;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by gao on 16-3-25.
*/
public class Test {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com\\ioc\\ch4_10\\beans.xml"); Car car1 = (Car) ctx.getBean("car");
Car car2 = (Car) ctx.getBean("car");
System.out.println(car1 == car2); //false
UserDao_2 ud1 = (UserDao_2) ctx.getBean("userDao_2");
UserDao_2 ud2 = (UserDao_2) ctx.getBean("userDao_2");
System.out.println(ud1 == ud2); //true
((ClassPathXmlApplicationContext)ctx).destroy();
}
}
Spring IoC — 基于注解的配置的更多相关文章
- Spring boot 基于注解方式配置datasource
Spring boot 基于注解方式配置datasource 编辑 Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...
- Spring IoC — 基于XML的配置
1.属性注入 注意点: 1)如果类中显示定义了一个带参的构造函数,则一定还要显示提供一个无参构造函数,否则使用属性注入时将抛出异常. 2)JavaBean关于属性命名的特殊规范.Spring只会检查B ...
- (spring-第4回【IoC基础篇】)spring基于注解的配置
基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现. 也就是说,加了注解,相当于在XML中配置了,一样 ...
- Spring框架bean的配置(3):基于注解的配置
1.基于注解的配置: @Component: 基本注解, 标识了一个受 Spring 管理的组件 @Respository: 标识持久层组件 @Service: 标识服务层(业务层)组件 @Contr ...
- Spring IoC — 基于Java类的配置
普通的POJO只要标注@Configuration注解,就可以为Spring容器提供Bean定义的信息了,每个标注了@Bean的类方法都相当于提供一个Bean的定义信息. 基于Java类的配置方法和基 ...
- Spring基于注解@Required配置
基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...
- Spring 基于注解零配置开发
本文是转载文章,感觉比较好,如有侵权,请联系本人,我将及时删除. 原文网址:< Spring 基于注解零配置开发 > 一:搜索Bean 再也不用在XML文件里写什么配置信息了. Sprin ...
- Spring 基于注解的配置 简介
基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...
- Spring:基于注解的Spring MVC
什么是Spring MVC Spring MVC框架是一个MVC框架,通过实现Model-View-Controller模式来很好地将数据.业务与展现进行分离.从这样一个角度来说,Spring MVC ...
随机推荐
- wpf MVVM ViewModel 关闭View显示
上次说到了不同wpf窗体之间的交互,这个在MVVM模式之中用起来会方便很多 下面我来说下在ViewModel中关闭View的方法,其实也很简单的,注释照样不写,一看就懂的 public partial ...
- 表达式语言之EL表达式
1.EL的用法EL的起源:起源于JSTL.EL运算符: 算术型:+.-.*./.div.%.mod.其中/和div都表示求除.%和mod表示求余数. 逻辑型:and或&&.or或||. ...
- css 动画效果
要搞就搞明白,一知半解时停止研究 损失最大 css3意义: CSS3 动画 通过 CSS3,我们能够创建动画,这可以在许多网页中取代动画图片.Flash 动画以及 JavaScript. 重点 ...
- 总线(BUS)和总线操作
1.什么是总线? 答:总线是运算部件之间数据流通的公共通道. 2.总线的作用? 答:提高专用信号处理逻辑电路的运算能力和速度. 3.总线与部件之间是怎么连接的? 答:各运算部件和数据寄存器组是通过带控 ...
- Ubuntu 14.04 安装 Xilinx ISE 14.7 全过程
生命在于折腾. 这个帖子作为我安装xilinx ISE 14.7版本一个记录.希望给需要的人一些帮助,这些内容绝大部分也是来源于互联网. 软硬件: lsb_release -a No LSB modu ...
- WPF 多线程处理(6)
WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) 以下是子窗体的UI: <Window ...
- linux 删除某种规则命名的文件
由于android开发需要删除以IMG_开头命名的图片文件,因此用到此命令 命令格式: rm IMG_*
- iOS Core data多线程并发访问的问题
大家都知道Core data本身并不是一个并发安全的架构:不过针对多线程访问带来的问题,Apple给出了很多指导:同时很多第三方的开发者也贡献了很多解决方法.不过最近碰到的一个问题很奇怪,觉得有一定的 ...
- svn: E155004: ..(path of resouce).. is already locked
svn: E155004: ..(path of resouce).. is already locked I'm getting an error when trying to commit a c ...
- c++ 哪些自定义的数据类型
http://www.cnblogs.com/ShaneZhang/archive/2013/06/21/3147648.html 这些数据类型是 C99 中定义的,具体定义在:/usr/includ ...