梦相随1006

版权归 梦相随1006 所有,未经 https://www.cnblogs.com/xin1006 作者许可,严禁转载

导航

Spring01-IOC

1,新建一个Java Project 导入如下jar包和apache-commons-logging包

 2, 在src目录下创建一个beans.xml的配置文件,先加入schema

spring-beans-3.2.4.RELEASE.jar/org.springframework.beans.factory.xml/spring-beans-version.xsd文件,可以放到本地的dtd目录中,然后引入.

<?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">

<!-- 相当于 HelloWorld hello =new HelloWorld()-->
<bean id="hello" class="com.yangw.spring.model.HelloWorld"></bean>

</beans>

 

3,创建一个对象HelloWorld

package com.yangw.spring.model;

public class HelloWorld {

    public String hello(){
        return "hello world";
    }
}

4,在beans.xml中创建对象

<!-- 相当于 HelloWorld hello =new HelloWorld()-->
<bean id="hello" class="com.yangw.spring.model.HelloWorld"></bean>

5,创建测试类,完成测试

package com.yangw.spring.test;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yangw.spring.model.HelloWorld;

public class TestSpring {
    @Test
    public void test01(){
        
        //创建Spring工厂
        BeanFactory factory= new ClassPathXmlApplicationContext("beans.xml");
        //通过工厂获取Spring的对象
        HelloWorld hello = factory.getBean("hello", HelloWorld.class);// 这个字符串参数是bean.xml中的id
        System.out.println(hello.hello());
    }
}

 6, scope属性的用处

<!-- bean标签中有一个属性 scope用来表示范围的 (singleton单例[默认],prototype多例)-->
<bean id="hello" class="com.yangw.spring.model.HelloWorld" ></bean>

7,下面是一个User的action-service-dao的完整过程演示

   1)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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<!-- bean标签中有一个属性 scope用来表示范围的 (singleton单例[默认],prototype多例)-->

<!-- 将所有的类交给Spring管理,并且对需要注入的完成依赖注入 -->
<bean id="userDao" class="com.yangw.spring.dao.UserDao" />
<bean id="userService" class="com.yangw.spring.service.UserService">
    <!-- name中的值会在userService对象中调用setXXX()方法完成注入 -->
    <property name="userDao" ref="userDao" />
</bean>
<!-- 对于userAction而言,里面的属性值状态会根据不同的线程得到不同的值,因此应该使用多例 -->
<bean id="userAction" class="com.yangw.spring.action.UserAction" scope="prototype">
    <property name="userService" ref="userService" />
</bean>


</beans>

    2)User对象

package com.yangw.spring.model;

public class User {

    private int id;
    private String username;
    
    public User(int id, String username) {
        
        this.id = id;
        this.username = username;
    }
    public User() {
    
    }
    
    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 "User [id=" + id + ", username=" + username + "]";
    }
    
    
}

    3)UserDao ,接口略去不再这里写了

package com.yangw.spring.dao;

import com.yangw.spring.model.User;

public class UserDao implements IUserDao {

    @Override
    public void add(User user) {
        
        System.out.println("add :"+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;
    }

}

    4)UserService

package com.yangw.spring.service;

import com.yangw.spring.dao.IUserDao;
import com.yangw.spring.model.User;

public class UserService implements IUserService {

    private IUserDao 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);
        
    }

    public IUserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(IUserDao userDao) {
        this.userDao = userDao;
    }
    

}

    5)UserAction

package com.yangw.spring.action;

import com.yangw.spring.model.User;
import com.yangw.spring.service.IUserService;

public class UserAction {

    private int id;
    private User user;
    private IUserService userService;
    
    public void add(){
        userService.add(user);
    }
    
    public void delete(){
        userService.delete(id);
    }
    
    public void load(){
        User u=userService.load(id);
        System.out.println(u);
    }

    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;
    }

    public IUserService getUserService() {
        return userService;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }
    
    
}

    6)测试

package com.yangw.spring.test;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yangw.spring.action.UserAction;
import com.yangw.spring.model.HelloWorld;
import com.yangw.spring.model.User;

public class TestSpring {
    
     //1,创建Spring工厂
    BeanFactory factory= new ClassPathXmlApplicationContext("beans.xml");
    
    
    @Test
    public void testUser(){
        
       
        //2,通过工厂获取Spring的对象
        UserAction userAction = factory.getBean("userAction", UserAction.class);
       
        User u1=new User(1,"yangw");
        userAction.setUser(u1);
        userAction.add();
    }
}


8, 一些简单属性的注入使用 value属性而不是ref属性

<bean id="user" class="com.yangw.spring.model.User">
    <!-- 为user设置一些属性值,会调用对象的setXXX()注入值,使用value属性 -->
    <property name="id" value="1" />
    <property name="username" value="tiantian" />
    <!-- 还可以注入list这种列表 -->
    <property name="names">
        <list>
            <value>11111</value>
            <value>22222</value>
            <value>33333</value>
        </list>
    </property>
</bean>
<!-- 对于userAction而言,里面的属性值状态会根据不同的线程得到不同的值,因此应该使用多例 -->
<bean id="userAction" class="com.yangw.spring.action.UserAction" scope="prototype">
    <property name="userService" ref="userService" />
    <property name="user" ref="user"></property>
</bean>

9, 基于annotation的注入

<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">

   <!--1, 打开Spring的annotation -->
   <context:annotation-config/>
   <!-- 2, 设定Spring去那些包中找annotation -->
    <context:component-scan base-package="com.yangw.spring" />
</beans>
//相当于 <bean id="userDao" class="com.yangw.spring.dao.UserDao"/>
//Component是通用的,一般dao层我们使用
//@Component("userDao")
@Repository("userDao")
public class UserDao implements IUserDao {

}
@Service("userService")
public class UserService implements IUserService {

    @Resource
    //@Autowired 建议使用Resource,Resource默认通过名称注入
    private IUserDao userDao ;
    //其实也可以在set方法上面进行注入    

}

 

@Controller("userAction")
@Scope("prototype")
public class UserAction {

    private int id;
    private User user;
    @Resource
    private IUserService userService;
    
}

public class TestSpring2 {
    
     //1,创建Spring工厂
    BeanFactory factory= new ClassPathXmlApplicationContext("beans2.xml");
    
   
    @Test
    public void testUser(){
        
       
        //2,通过工厂获取Spring的对象
        UserAction userAction = factory.getBean("userAction", UserAction.class);
       
        User u1=new User(1,"yangw");
        userAction.setUser(u1);
        userAction.add();
    }
}

 

 

 

posted on 2013-10-18 16:27  梦相随1006  阅读(208)  评论(0编辑  收藏  举报