Spring从IOC(Inversion of Control:控制反转)开始
引用
有的时候变的茫然了,不知道前方的路该怎么走;有的时候变的颓废了,不知道从前的你过的还是否开心;有的时候理解和包容同在,我愿意做一个更出色的男人。辗转反侧通往成功的大门还需要多少力气敲开,于是静下心来回顾与内省。
Spring是不是个好东西我不知道,但当大家都在推崇的时候,那么即使再默然,也应该警醒,即便大家不一定对,但天若塌下来,那大家也会一起陪着,你并不孤单
ioc有两种实现方式
第一种xml的方式实现
首先建个基类:
public abstract class People {
protected int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
再然后创建teacher类
public class Teacher extends People {
private String name;
private String tNo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String gettNo() {
return tNo;
}
public void settNo(String tNo) {
this.tNo = tNo;
}
@Override
public String toString() {
return "id = "+id
+" name = "+ name
+" tNo = " + tNo;
}
}
在src下面创建beans.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">
<!--相当于 Teacher teacher = new Teacher() scope 为 prototype多例,默认为单例-->
<bean id="teacher" class="com.model.xml.Teacher" scope="prototype">
<property name="id" value="10"/>
<property name="name" value="teacher"/>
<property name="tNo" value="t10"/>
</bean>
</beans>
编写测试代码Test.java
public class Test {
private BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
@org.junit.Test
public void testTeacherXml(){
Teacher teacher = factory.getBean("teacher",Teacher.class);
System.out.println(teacher.toString());
}
}
输出信息:
id = 10 name = teacher tNo = t10
再来创建一个
创建User类
public class User {
private int id;
private String userName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Override
public String toString() {
return "id = " + id
+" userName = " + userName;
}
}
创建UserAction
public class UserAction {
private int id;
private User user;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
@Override
public String toString() {
return "userAction.id = " + id
+ " " + user.toString();
}
}
因为UserAction持有了User对象那么看下beans.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">
<bean id="user" class="com.model.xml.User">
<property name="id" value="2"/>
<property name="userName" value="haha"/>
</bean>
<!--注入对象使用ref 注入值使用value-->
<bean id="userAction" class="com.model.xml.UserAction" scope="prototype">
<property name="id" value="12"/>
<property name="user" ref="user"/>
</bean>
</beans>
编写测试代码:
public class Test {
private BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
@org.junit.Test
public void testUserActionXml(){
UserAction userAction = factory.getBean("userAction",UserAction.class);
System.out.println(userAction.toString());
}
}
输出信息:
userAction.id = 12 id = 2 userName = haha
第二种采用注解的方式实现
注解的标注有以下几种
@Component、@Repository、@Service、@Controller、@Autowired、@Resource
其中代表的含义如下
@Component泛指组件,可以在一些POJO上使用这个注解进行标注
@Repository用于标注数据访问组件,即DAO组件
@Service用于标注业务层组件
@Controller用于标注控制层组件,如Struts中的Action
@Autowired Spring提供装配
@Resource j2EE提供
接下来先创建Student类使用Component注解
@Component
public class Student extends People {
private String name;
private String sNo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getsNo() {
return sNo;
}
public void setsNo(String sNo) {
this.sNo = sNo;
}
@Override
public String toString() {
return "id = "+id
+" name = "+ name
+" sNo = " + sNo;
}
}
在beans.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">
<!--打开Spring的Annoation支持-->
<context:annotation-config/>
<!--扫描model包下面以及子包下面的所有类-->
<context:component-scan base-package="com.model.**"/>
</beans>
编写Test.java测试代码:
public class Test {
private BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
@org.junit.Test
public void testAnno(){
Student student = factory.getBean("student",Student.class);
student.setId(20);
student.setName("teacher");
student.setsNo("s20");
System.out.println(student.toString());
}
}
输出信息:
id = 20 name = teacher sNo = s20