java之spring之scope和autowiring

目录结构如下:

UserDao.java

1 package cn.sxt.dao;
2 
3 public interface UserDao {
4     public void add();
5 }
View Code

UserDaoImpl.java

 1 package cn.sxt.dao.impl;
 2 
 3 import cn.sxt.dao.UserDao;
 4 
 5 public class UserDaoImpl implements UserDao{
 6     @Override
 7     public void add() {
 8         System.out.println("User  add");
 9     }
10 }
View Code

UserDaoOracleImpl.java

 1 package cn.sxt.dao.impl;
 2 
 3 import cn.sxt.dao.UserDao;
 4 
 5 public class UserDaoOracleImpl implements UserDao{
 6     @Override
 7     public void add() {
 8         System.out.println("User  add");
 9     }
10 }
View Code

UserService.java

package cn.sxt.service;

public interface UserService {
    public void add();
}
View Code

UserServiceImpl.java

 1 package cn.sxt.service.impl;
 2 
 3 import cn.sxt.dao.UserDao;
 4 import cn.sxt.service.UserService;
 5 
 6 public class UserServiceImpl implements UserService{
 7     private UserDao userDao;
 8     public void setUserDao(UserDao userDao) {
 9         this.userDao = userDao;
10     }
11     @Override
12     public void add() {
13         userDao.add();
14     }
15 }
View Code

beans.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5         http://www.springframework.org/schema/beans/spring-beans.xsd">
 6   
 7   <bean id="userDao" class="cn.sxt.dao.impl.UserDaoImpl"></bean>
 8   <bean id="userService" class="cn.sxt.service.impl.UserServiceImpl">
 9       <property name="userDao" ref="userDao"></property>
10   </bean>
11 </beans>
View Code

SpringTest.java

 1 package cn.sxt.spring;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import cn.sxt.service.UserService;
 8 
 9 public class SpringTest {
10     @Test
11     public void testHello(){
12         //��������
13         ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
14         UserService u1=(UserService)ac.getBean("userService");
15         u1.add();
16     }
17 }
View Code

 

一. scope: 域 ; 在spring中由容器创建对象时可以以指定其scope,那么容器会根据不同的scope的值,创建指定域的对象。

  Singleton : 单例(默认),在容器中只有一个该类型的对象。

  Prototype : 原型, 每次从容器中获取对象时,都重新创建一个对象;action 必须设为 prototype 。

  Request:创建一个对象,该对象存于请求范围。

  Session : 创建一个对象,该对象和session范围一致。

  Application :  创建一个对象,该对象存于 servletContext 中。

二. autowiring : 自动装配

1. no : 默认,不使用自动装配

2. byName : 设置为 byName ,那么在创建该对象时,会根据该对象的 set 方法到容器中查找是否有对应的标识符对象存在,如果存在则注入该对象。

配置文件 :beans.xml

<bean id="userDao" class="cn.sxt.dao.impl.UserDaoImpl"></bean>
  <bean id="userService" class="cn.sxt.service.impl.UserServiceImpl" autowire="byName">
  </bean>

UserServiceImpl.java 文件

package cn.sxt.service.impl;

import cn.sxt.dao.UserDao;
import cn.sxt.service.UserService;

public class UserServiceImpl implements UserService{
    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    @Override
    public void add() {
        userDao.add();
    }
}

3. byType : 在创建对象时,会根据该对象的 set 方法的参数类型在容器中查找,如果找到该类型的对象则直接注入,找不到则不注入。

注意:使用 byType 注入,同一个类型在容器中只能有一个配置

4. constructor:本质是 byType注入,只不过是通过对象的构造器去注入,而不是 set 方法。

5. 可以通过头文件,设置全局的自动装配类型:

<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.xsd" default-autowire="byName">

自动装配容器出错,不建议使用

 

posted @ 2019-07-26 07:41  Vincent-yuan  阅读(300)  评论(0编辑  收藏  举报