Spring (Dependency Injection 2)

1. XML-based bean injection
1. Constructor
injection
2. Set method injection 2. Automatic injection of Bean [Spring auto-assembly strategy]
Automatic injection [Auto-assembly]-Spring container will automatically depend on the elements configured in the configuration file Objects are injected into member variables in the caller class.
To use automatic assembly, you need to configure the autowire attribute of the element.
Insert picture description here

For example: test byName [the id attribute value of the total bean element of the Spring configuration file is the same as the member variable name of the dependent object in the caller class]
Note: the dependent object needs to be provided with setXX()

package com.wangxing.spring.byname;
//被调用者类
public class PersonDao {
    public void select(){
        System.out.println("查询所有用户信息");
    }
}

package com.wangxing.spring.byname;
//调用者类
public class PersonService {
    //定义依赖对象
    private PersonDao personDao;
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }
    public  void  testPersonService(){
        personDao.select();
    }
}

<?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.xsd">
    <!-- 测试byname自动注入方式-->
    <bean id="personDao" class="com.wangxing.spring.byname.PersonDao"></bean>
    <bean id="personService" class="com.wangxing.spring.byname.PersonService" autowire="byName"></bean>
</beans>

For example: test byType [the class attribute value of the bean element in the Spring configuration file is the same as the type of the dependent object member variable in the caller class]
Note: You need to provide setXX() for the dependent object

package com.wangxing.spring.bytype;
//被调用者
public class StudentDao {
    public void selectStudent(){
        System.out.println("查询所有学生信息");
    }
}

package com.wangxing.spring.bytype;
//调用者
public class StudentService {
    //定义依赖对象
    private StudentDao studentDao;
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }
    public  void   testStudentService(){
        studentDao.selectStudent();
    }
}

<?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.xsd">
    <!--测试bytype自动注入方式-->
    <bean id="stuDao" class="com.wangxing.spring.bytype.StudentDao"></bean>
    <bean id="studentService" class="com.wangxing.spring.bytype.StudentService" autowire="byType"></bean>
</beans>

For example: test constructor [the class attribute value of the bean element in the Spring configuration file is the same as the parameter type of the constructor in the caller class {byType}]

package com.wangxing.spring.constructor;
//被调用者
public class UserDao {
    public void  selectUser(){
        System.out.println("查询所有用户信息");
    }
}
package com.wangxing.spring.constructor;
//调用者类
public class UserService {
    private UserDao userDao;
    public UserService(UserDao userDao){
        this.userDao=userDao;
    }
    public  void  testUserService(){
        userDao.selectUser();
    }
}

<?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.xsd">    
<!--测试constructor自动注入方式-->
    <bean id="uDao" class="com.wangxing.spring.constructor.UserDao"></bean>
    <bean id="userService" class="com.wangxing.spring.constructor.UserService" autowire="constructor"></bean>
</beans>

3. Bean injection based on annotations
In Spring, although the use of XML configuration files can achieve the assembly of Beans, if the number of Beans in the application is large, the XML configuration files will be too bloated, which will bring certain difficulties to maintenance and upgrades. .
Java has provided Annotation (annotation) function since JDK 5.0, and Spring also provides comprehensive support for Annotation technology. A series of Annotations (annotations) are defined in Spring3.
1) @Component creates an annotation for an object. When using it, you only need to mark the annotation on the corresponding class.
Before @Component

public class  Student{
}
Spring配置文件
<bean id=”student” class=”com.wangxing.spring.bean.Student”></bean>
有@Component之后
@Component----使用默认的对象名称【类名,首字母小写】
public class  Student{
}
@Component(“stu”)---使用指定的对象名称
public class  Student{
}

E.g:

package com.wangxing.spring.annotation;
import org.springframework.stereotype.Component;
@Component("user")
public class UserBean {
    public void  testUserBean(){
        System.out.println("UseerBean类的是实例方法");
    }
}

<?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.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!--使用context命名空间,通知spring扫描指定目录,进行注解的解析-->
    <context:component-scan base-package="com.wangxing.spring.annotation"></context:component-scan>
</beans>

2) @Repository annotates the data access interface implementation class, and its function is the same as @Component.
3) @Service marks the business access interface implementation class, and its function is the same as @Component.
4) @Controller annotates the control layer implementation class, and its function is the same as @Component.
5) @Autowired completes the automatic injection of Bean. The default is bytype.

package com.wangxing.spring.annotation;
import org.springframework.stereotype.Component;
@Component("user")
public class UserBean {
    public void  testUserBean(){
        System.out.println("UseerBean类的是实例方法");
    }
}

package com.wangxing.spring.annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository("userDao")
public class UserDao {
    //定义依赖对象UserBean
    @Autowired
    private UserBean userBean;

    public void  testUserDao(){
        System.out.println("数据访问层接口实现类");
        userBean.testUserBean();
    }
}

6) @Resource completes the automatic injection of Bean. The default is according to byname.

package com.wangxing.spring.annotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository("uDao")
public class UserDao {
    //定义依赖对象UserBean
    @Autowired
    private UserBean user;
    public void  testUserDao(){
        System.out.println("数据访问层接口实现类");
        user.testUserBean();
    }
}

package com.wangxing.spring.annotation.service;
import com.wangxing.spring.annotation.UserDao;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("userService")
public class UserService {
    //定义依赖对象UserDao
    @Resource
    private UserDao userDao;

    public void  testUserService(){
        System.out.println("业务层接口实现类");
        userDao.testUserDao();
    }
}

What is the difference between @autowired and @resource annotations?
(1) Both @Autowired and @Resource can be used to assemble beans, and both can be written on fields or setter methods.
(2) @Autowired is assembled by type by default, and dependent objects must be required by default Exist, if you want to allow a null value, you can set its required attribute to false. If you want to use the name assembly, you can use it in conjunction with the @Qualifier annotation.
(3) @Resource, the default is to assemble according to the name, the name can be specified by the name attribute, if the name attribute is not specified, when the annotation is written on the field, the field name is used by default for name search. If the annotation is written on the setter method, the attribute name is used for assembly by default. When no bean matching the name is found, the assembly is performed according to the type. But it should be noted that if the name attribute is specified, it will only be assembled according to the name.
It is recommended to use @Resource annotation on the field, so that there is no need to write setter methods, and this annotation belongs to J2EE, reducing the coupling with Spring.
7) The use of @Qualifier and @Autowired annotations will modify the default assembly by Bean type to assembly by the instance name of the Bean. The instance name of the Bean is specified by the parameter of the @Qualifier annotation.

public interfase  UserDao{
}
@Repository(“studentUsetDao”)
public class StudentUserDao implements UserDao{
}
@Repository(“personUsetDao”)
public class PersonUserDao implements UserDao{
}
//业务类
public  class  UserServiceImple{
@Autowired
@Qualifier(“personUsetDao”)
  private UserDao  userDao;
}

Guess you like

Origin blog.csdn.net/guoguo0717/article/details/109845108