1、导入spring的dist中的jar包和commons-logging包
2、在src目录下创建相应的beans.xml
3、为beans.xml添加相应的schema
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
</beans>
4、新建一个类
5、在beans.xml中创建对象
<!--
创建如下bean等于完成了:HelloWorld helloWorld = new HelloWorld
-->
<bean id="helloWorld" class="org.zttc.itat.spring.model.HelloWorld"/>
6、在测试类中使用这个对象
6.1、创建Spring的工厂
private BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
6.2、通过Spring工厂获取相应的对象
//此处getBean中的helloWorld就是beans.xml配置文件中的id
HelloWorld hello = factory.getBean("helloWorld",HelloWorld.class);
//此时的hello对象就是被Spring说管理的对象
System.out.println(hello.hello());
代码如下:
model:
package org.zttc.itat.spring.model;
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;
}
public User() {
}
public User(int id, String username) {
super();
this.id = id;
this.username = username;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + "]";
}
}
dao:
//抽象类
package org.zttc.itat.spring.dao;
import org.zttc.itat.spring.model.User;
public interface IUserDao {
public void add(User user);
public void delete(int id);
public User load(int id);
}
//实现类
package org.zttc.itat.spring.dao;
import org.zttc.itat.spring.model.User;
public class UserDao implements IUserDao {
@Override
public void add(User user) {
System.out.println("添加了"+user);
}
@Override
public void delete(int id) {
System.out.println("delete :"+id);
}
@Override
public User load(int id) {
System.out.println("load:"+id);
return null;
}
}
service:
//抽象类
package org.zttc.itat.spring.service;
import org.zttc.itat.spring.model.User;
public interface IUserService {
public void add(User user);
public void delete(int id);
public User load(int id);
}
//实现类
package org.zttc.itat.spring.service;
import org.zttc.itat.spring.dao.IUserDao;
import org.zttc.itat.spring.model.User;
public class UserService implements IUserService {
//此处类是要依靠spring来注入的。
private IUserDao userDao;
//相应的get set 方法,这个必须有
public IUserDao getUserDao() {
return userDao;
}
public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
}
@Override
public void add(User user) {
userDao.add(user);
}
@Override
public void delete(int id) {
userDao.delete(id);
}
@Override
public User load(int id) {
return userDao.load(id);
}
}
action:
package org.zttc.itat.spring.action;
import java.util.List;
import org.zttc.itat.spring.model.User;
import org.zttc.itat.spring.service.IUserService;
public class UserAction {
//此处四个也是要依赖spring的注入
private User user;
private IUserService userService;
private int id;
private List<String> names;
public List<String> getNames() {
return names;
}
public void setNames(List<String> names) {
this.names = names;
}
public IUserService getUserService() {
return userService;
}
public void setUserService(IUserService userService) {
this.userService = userService;
}
public UserAction() {
}
public UserAction(IUserService userService) {
super();
this.userService = userService;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
//public IUserService getUserService() {
//return userService;
//}
//
//public void setUserService(IUserService userService) {
//this.userService = userService;
//}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void add() {
System.out.println(id);
System.out.println(names);
userService.add(user);
}
public void delete() {
userService.delete(id);
}
public void load() {
userService.load(id);
}
}
testSpring:
package org.zttc.itat.spring.test;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.zttc.itat.spring.action.UserAction;
import org.zttc.itat.spring.model.HelloWorld;
import org.zttc.itat.spring.model.User;
public class TestSpring {
//创建Spring的工厂
private BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
@Test
public void testHello() {
//通过工厂获取Spring的对象
//此处getBean中的helloWorld就是beans.xml配置文件中的id
//HelloWorld hello = (HelloWorld)factory.getBean("helloWorld");
HelloWorld hello = factory.getBean("helloWorld",HelloWorld.class);
//此时的hello对象就是被Spring说管理的对象
System.out.println(hello.hello());
HelloWorld hello2 = factory.getBean("helloWorld",HelloWorld.class);
//如果在bean中没有作scope的配置,默认是singleton(单例),当把bean中的scope设置为prototype的时候就是多例
System.out.println(hello==hello2);
}
@Test
public void testUser01() {
UserAction ua1 = factory.getBean("userAction", UserAction.class);
User u = new User(1,"悟空");
ua1.setUser(u);
ua1.add();
UserAction ua2 = factory.getBean("userAction", UserAction.class);
ua2.add();
}
}
beam.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 创建了一个User对象user,id为1,username为 八戒 ,如果要注入值不使用ref而是使用value -->
<bean id="user" class="org.zttc.itat.spring.model.User">
<property name="id" value="2"/>
<property name="username" value="八戒"/>
</bean>
<!-- 创建了一个userDao>
<bean id="userDao" class="org.zttc.itat.spring.dao.UserDao"></bean>
<!-- autowire=byName表示会根据name来完成注入,
byType表示根据类型注入 ,使用byType注入如果一个类中有两个同类型的对象就会抛出异常
所以在开发中一般都是使用byName
虽然自动注入可以减少配置,但是通过bean文件无法很好了解整个类的结果,所以不建议使用autowire-->
<!--创建一个userService-->
<bean id="userService" class="org.zttc.itat.spring.service.UserService">
<!-- name中的值会在userService对象中调用setXX方法来注入,诸如:name="userDao"
在具体注入时会调用setUserDao(IUserDao userDao)来完成注入
ref="userDao"表示是配置文件中的bean中所创建的DAO的id -->
<property name="userDao" ref="userDao"></property>
</bean>
<!-- 对于UserAction而言,里面的属性的值的状态会根据不同的线程得到不同的值,所以应该使用多例 -->
<bean id="userAction" class="org.zttc.itat.spring.action.UserAction" scope="prototype">
<property name="userService" ref="userService"/>
<property name="user" ref="user"/>
<property name="id" value="12"/>
<!-- 同样可以注入列表,但是也不常用 -->
<property name="names">
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</list>
</property>
</bean>
<!-- 以下是使用构造函数来注入,不常用,基本都是使用set方法注入 -->
<!-- <bean id="userAction" class="org.zttc.itat.spring.action.UserAction" scope="prototype">
<constructor-arg ref="userService"/>
</bean> -->
</beans>