3 spring series annotation-based IOC

ioc spring in common comment

 <bean id="accountService" class="com.mantishell.service.impl.AccountServiceImpl"
       scope="" init-method="" destroy-method="">
     <property name="" value="" | ref=""/>
 </bean>

Componment

Used to create objects, and the role of The same label

  • Component:
    Role: the current class objects stored in the spring container
    attributes: value- for specifying the bean id. When we do not write, its default value is the current class name and the first letter lowercase
  • Controller: generally used in the presentation layer
  • Service: Generally used in the business layer
  • Repository: persistence layer is generally used in
    the role of these three notes, the same properties and Componment
    three of them are spring framework provides a clear use for our three notes, the three objects more clearly

Note the use of annotations, you need to replace the configuration file header information as follows:

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

    <!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为
    context名称空间和约束中-->
    <context:component-scan base-package="com.mantishell"/>
</beans>

Persistence
IAccountDao.java

package com.mantishell.dao;
public interface IAccountDao {
    void saveAccount();
}

AccountDaoImpl.java

package com.mantishell.dao.impl;
import com.mantishell.dao.IAccountDao;
import org.springframework.stereotype.Repository;

@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
    public void saveAccount() {
        System.out.println("保存了账户");
    }
}

Business layer
IAccountService.java

package com.mantishell.service;
public interface IAccountService {
    void saveAccount();
}

AccountServiceImpl.java

package com.mantishell.service.impl;
import com.mantishell.dao.IAccountDao;
import com.mantishell.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
    @Autowired
    private IAccountDao accountDao = null;
    public void saveAccount() {
        accountDao.saveAccount();
    }
}

Presentation layer

package com.mantishell.ui;
import com.mantishell.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * 模拟一个表现层,用于调用业务层
 */
public class Client {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        IAccountService as = (IAccountService) ac.getBean("accountService");
        as.saveAccount();
    }
}

Autowired

For injecting data
bean tag role and xml configuration file in writing The same effect

  • Autowired:
    • Action: according to the type of automatic injection. As long as there is only one type of bean objects and variables to be injected into the match type of container you can inject success
      • If the container ioc no matching variable type and the type of bean to be injected, it is given.
      • If there are multiple types match Ioc vessel:
    • Appear here: the variables can be, it can be a method
    • Details: When using annotation injection, set method is not a must.
  • Qualifier:
    • Effect: by name and then injected on the basis of the class according to the above injection. It must be used together at the time of injection to the class members and Autowired. Alone may be used when a method of injection parameters.
    • Attribute: value: specifies the bean injected id.
  • Resource
    • Action: direct injection according to the bean id. It can be used independently
    • Attribute: name: used to specify the bean id.
      More than three injection bean can only inject other types of data, and the basic types and String types can not be used to achieve the above comment.
      In addition, the collection type of injection can only be achieved through XML.
  • Value
    • Action: String for injecting basic types and data types
    • Attribute: value: a value for the specified data. It may be used in the spring of SpEL (i.e. the spring el expression)
      of SpEL wording: Expression $ {}
    @Autowired
    @Qualifier("accountDao1")//指定使用id=accountDao1的bean对象
    private IAccountDao accountDao = null;

或者

    @Resource(name="accountDao2")//指定使用id=accountDao2的bean对象
    private IAccountDao accountDao = null;

Scope

Changing the scope
same role and function bean scope attribute tags, common values: singleton prototype

@Scope("singleton")
public class AccountServiceImpl implements IAccountService {

PreDestry and PostConstruct

Bean and acts like a tag-init-method Methode and the destroy
The PreDestroy: specifies destruction method
PostConstruct: a designated initializer

    @PostConstruct
    public void init(){
        System.out.println("初始化方法执行了");
    }

    @PreDestroy
    public void destroy(){
        System.out.println("销毁方法执行了");
    }

Guess you like

Origin www.cnblogs.com/mantishell/p/12557867.html
Recommended