Spring IOC container assembly annotation-based configuration Bean_

bean instantiation

1. Import jar package (essential)

2. instantiated bean

  • applicationContext.xml (wording of xml)
<bean id="userDao" class="com.igeekhome.dao.impl.UserDao"></bean>
  • Notes wording
  1. Open comments in the applicationContext.xml scan (while introducing context namespace)
<?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 https://www.springframework.org/schema/context/spring-context.xsd">

    <!--开启注解扫描
    context:component-scan 配置spring ioc容器开启注解扫描,扫描指定包下的@Component修饰的类,将这些类的对象创建交给spring ioc容器完成
    base-package: 需要扫描哪些包(及其子包)
    -->
    <context:component-scan base-package="com.igeekhome"></context:component-scan>
</beans>
  1. @ Component, @ Service, @ Controller, @ Repository used to instantiate bean

    Spring3.0 for us to introduce the automatic scanning mechanism components, it can look for marked @ Component, @ Service, @ Controller , @ Repository annotation class under the class path, and to incorporate these classes into the spring container management
    @ Service, @ Controller, @ Repository is derived @Component sub notes

  • @Repository for annotation data access component (e.g., layer assembly DAO)
  • @Service label for the business layer components (e.g., Service Layer)
  • Labeling assembly for controlling @Controller layer (e.g., layer struts in action)
  • @Component refers to the component when the component is not classified, we can use this annotation to mark
/*
注解:添加在实现类上(思想同配置文件)
@Component 等价于  <bean id="userDaoImpl" class="com.igeekhome.dao.impl.UserDaoImpl"></bean>
id:默认是类名(首字母小写) value:bean的id
@Component(value = "userDao")
@Component("userDao")
 */
@Repository("userDao")
public class UserDaoImpl implements IUserDao {
    public void select() {
        System.out.println("UserDao...select...");
    }
}

Dependency injection bean properties

Simple data types dependency injection

After Spring 3.0, provided @Value annotation data can be done simply injected

/*
@Component 
@Component(value="customerService")
@Service(value="customerService")
四者都等价
*/
@Service("customerService")
public class CustomerService {
    //简单类型的成员变量
    @Value("Rose")//参数的值简单类型
    private String name="Jack";
    
    //保存业务方法
    public void save(){
       System.out.println("CustomerService业务层被调用了。。。");
       System.out.println("name:"+name);
    }
}

Complex type data dependency injection

1. @Value binding SpEL - after spring3.0 with

@Service("userService")
public class UserServiceImpl implements IUserService {
   /*<bean id="userDao" class="com.igeekhome.dao.impl.UserDao">           </bean>
      @Value("#{bean的id}")
     */
     //写法一 (在属性声明上面注入,底层自动还是生成setUserDao())
    @Value("#{userDao}")
    private UserDaoImpl userDao;

    //写法二
     @Value("#{userDao}")
    public void setUserDao(UserDaoImpl userDao) {
        this.userDao = userDao;
    }
    
    public void list() {
        System.out.println("UserServiceImpl...list...");
        userDao.select();
    }
}

2. Use @Autowired combination @Qualifier

@Autowired: can be used alone, if used alone, carried out in accordance with the type of injection (looks in the spring ioc container com.igeekhome.dao.impl.UserDaoImpl type of bean and inject, if not, certainly injected a failure; if found bean single matching, the successful injection; if a plurality of the same type of bean is found, then select a bean implanting

@Qualifier: binding @Autowired, implanting by name

@Autowired//默认按照类型注入的
@Qualifier("userDao")//必须配合@Autowired注解使用,根据名字注入
private UserDaoImpl userDao;

3.JSR-250 standard (based JDK) provides annotations @Resource

If you do not specify a name attribute, then the first injection carried out by name (modified annotation attribute name) and look for the bean name / id in a container for the userDao of bean, if found, the injection was successful. If the name is not found in accordance with the injection corresponding to the bean, the assembly will be used in accordance with the type, if there is no change type, the injection failure, an exception is thrown; if a single type of the bean container is present, the successful injection; if the container there are a plurality of the same type bean, the injection failure (expected single matching bean but found 2: userDaoxxx, userDaox)

If the name attribute is specified, then it will be injected in accordance with the name

@Resource//(name="userDao")
private UserDaoImpl userDao;

4.JSR-330 standard (jdk) and provide @Inject @Named comment (not recommended)

The need to import the jar javax.inject

Use @Inject, default in accordance with the type of injection, and the use of @inject @Named notes, then injected by name. Usage and consistent usage of @Autowired combination @Qualifier

bean initialization and destruction

1.applicationContext.xml in the wording

<!--
    init-method:指定初始化方法
    destroy-method: 指定销毁触发方法
    -->
    <bean id="lifecycle" class="com.igeekhome.bean.LifeCycleBean" scope="singleton" init-method="initMethod" destroy-method="destroyMethod"></bean> 

2. Notes wording

Use @PostConstruct notes, indicating the initialization method --- equivalent to init-method specifies the initialization method
using @PreDestroy notes, indicating the methods of destruction ---- destroy-method is equivalent to the specified object destruction method

@Repository("userDao")
public class UserDaoImpl implements IUserDao {
    public void select() {
        System.out.println("UserDao...select...");
    }

    //init-method
    @PostConstruct
    public void init() {
        System.out.println("init...");
    }
    //destory-method
    @PreDestroy
    public void destory() {
        System.out.println("destory");
    }
}

scope of the bean

1.applicationContext.xml in the wording

The default is singleton single embodiment, prototype multi Example

<bean id=”” class=”” scope=”prototype”>

2. Notes wording

By @Scope annotation, specify the scope of the Bean

@Service("userService")
//@Scope("singleton")默认是单列
@Scope("prototype")
public class UserServiceImpl implements IUserService { }

Remark

Only open the annotation scanning in the Spring configuration file
to use @Component @Autowired @Resource @PostConstruct @PreDestroy and other notes

Guess you like

Origin www.cnblogs.com/hublogs/p/11994886.html