The scope of the java spring and autowiring

Directory structure is as follows:

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

 

A scope: domain; can be specified in its scope, then the container will scope of different values, to create the object specified domain when creating objects by a container in the spring.

  Singleton: Singleton (default), only one object in a container of that type.

  Prototype: prototype, get the object each time a container from, have to re-create an object; action must be set to prototype.

  Request: create an object, the object is stored in the request scope.

  Session: create an object, consistent with the object and scope of the session.

  Application: Create an object stored in servletContext in.

Two autowiring:. Autowire

1. no: default, do not use automated assembly

2. byName: byName set, then the object is created, it will look into the container set according to whether or not there is a method of the object corresponding to the object identifier is present, if the object exists is injected.

Profile: 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 file

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:, looks for object creation method according to the parameters set type of the object in the container, if the object is to find the type of direct injection, can not find no injection.

Note: Use byType injection, the same type in the container can have a configuration

4. constructor: essentially byType injection, merely by constructing an object to injection, rather than set method.

The header can be set globally autowiring type:

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

Automatic container assembly error, is not recommended

 

Guess you like

Origin www.cnblogs.com/Vincent-yuan/p/11248025.html