Introduction to spring structure (1)

1. Definition and characteristics

  Definition: A sub-module one-stop backend development framework.

  feature:

  (1) Compared with EJB, it is a lighter container framework, organized in the form of modules, and only needs to call the corresponding modules (jdbc, springmvc)

  (2) Spring IOC low coupling and easy to combine the relationship between objects

  (2) AOP makes it easier to extend functions , and does not need to write irrelevant code into the main business

  (3) Spring does not directly build wheels, but uses existing technologies, such as ORM (object-relational mapping, mapping objects to databases) framework, logging framework;

2、  IOC/DI

  Inversion of control/dependency injection, the beans themselves do not depend on each other, the activities that generate the relationship do not depend on any party, and the entire dependency construction (injection) is managed by a third party.

  The implementation method of inversion of control is dependency injection. Party A opens the interface and allows Party B to pass in (injection) when it needs it.

  The traditional way is to build a dependent object B and inject it into A by creating a new object A in the program;

   

 

  The process of creating and injecting objects in Spring is handed over to the IOC container, that is, to dynamically provide an object with the dependent objects it needs. The basic technology is reflection.

  

  Advantage 1. It is not necessary to know the underlying class code (the underlying code of historical maintenance), and it can be injected directly when needed. In actual projects, some Service Classes may have been written ten years ago, with hundreds of classes as their bottom layers. Assuming that a newly written API needs to instantiate this Service, we need to understand a lot of historical code. This feature of IoC Container perfectly solves this kind of problem - because this architecture requires you to write the corresponding Config file when writing a class, so when you want to initialize the Service class a long time ago, the predecessors have already written Ok, the Config file, you can directly inject this Service where you need it. This greatly increases the maintainability of the project and reduces the difficulty of development.

  Advantage 2, resources are controlled by a third party, which reduces coupling and facilitates the configuration and management of resources.

3、 AOP

  At runtime, the programming idea of ​​dynamically inserting code into the specified method and position of the class is aspect-oriented programming.

  (Personal understanding of aop is to insert code unrelated to the main business into the entire logic through the proxy mode, without polluting the original functional structure)

4, bean configuration (bean registration )

  (1)    XML -based bean configuration (ApplicationContext)

  It is implemented using a series of XML tags supported by the Spring namespace, such as context, beans, jdbc, tx, aop, mvc, etc.      

       属性<property name="name" value="tom"></property>

  构造方法<constructor-arg value="Mike" name="name"></constructor-arg>

  (2)    Annotation-based bean configuration

  To move the bean description inside the component class, just use annotations on the relevant class, method or field declaration.

        @Component: refers to general-purpose components; used to annotate component classes

  @Repository: used to annotate DAO component classes;

  @Service: used to annotate the Service component class;

  @Controller: used to annotate the Controller component class;

         (3) bean configuration based on java class (configured through code)

         The class annotated by @Configuration indicates that the main purpose of this class is to define a resource as a bean similar to <beans>; the @Bean annotation annotates a method, telling the method to return an object, similar to <bean>. Ps: Compared with A, because it is java code, the compiler can check that the type is legal and the id is unique.

  The upper left of the figure below shows the basic syntax of java-based bean declaration, the upper right is how to declare the interdependence between beans, and the bottom is how to initialize and invoke the process.

 

 

 

5, bean injection (bean use, eg: the class contains a class object bean)

  (1)    Based on xml injection - attribute injection

  Attribute injection requires the bean to provide a default constructor and corresponding SetXXX methods for the attributes that need to be injected. Spring first calls the Bean's default constructor to instantiate the Bean object, and then calls the Setter method to inject property values ​​through reflection.

public class LogonService implements BeanNameAware{

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {

        this.userDao = userDao;

    }

}

 

<bean id="userDao" class="com.baobaotao.anno.UserDao"/>

<bean class="com.baobaotao.anno.LogonService">

    <property name="userDao" ref="userDao"></property>

</bean>

 

  (2)    Based on xml injection- construction method

  The premise of using constructor injection is that the bean must provide a constructor with parameters.

public class LogonService implements BeanNameAware{

    private UserDao userDao;

public LogonService(){}

    public LogonService(LogDao logDao, UserDao userDao) {

        this.userDao = userDao;

    }}

 

<bean id="userDao" class="com.baobaotao.anno.UserDao"/>

<bean class="com.baobaotao.anno.LogonService">

    <constructor-arg ref="userDao"></constructor-arg>

</bean>

 

  (3)    Based on annotation injection

  @required is annotated on a setting method to avoid forgetting to inject due to too many beans

  @Autowired has finer-grained assembly control

  @Qualifier(“userDao”)用于处理存在两个相同类型的bean时,自动装配产生歧义的问题

@Service

public class LogonService implements BeanNameAware{

    @Autowired(required=false)

    @Qualifier("userDao")

private UserDao userDao;}

 

6、 spring中bean的自动装配

  无须在Spring配置文件中描述JavaBean之间的依赖关系(如配置<property>、<constructor-arg>)。IOC容器会自动建立javabean之间的关联关系。

  (1)    no-默认情况下采用”ref”进行手动绑定;

  (2)    byname-根据名字进行装配

    

  (3)    byType-根据某个bean所持有的依赖变量的数据类型进行自动装配

    

  (4)    constructo-与byType类似,只不过是按照构造函数的参数类型进行对应bean匹配

  (5)    autodetect-自动使用constructor和byType进行自动装配

 

7、 Spring作用域

  结构:<bean id="userDao" class="com.ioc.UserDaoImpl" D="singleton"/>

  singleton:单例模式,Spring IoC容器中只会存在一个共享的Bean实例,无论有多少个Bean引用它,始终指向同一对象。Singleton作用域是Spring中的缺省作用域,也可以显示的将Bean定义为singleton模式,配置为:

  prototype:原型模式,每次通过Spring容器获取prototype定义的bean时,容器都将创建一个新的Bean实例,每个Bean实例都有自己的属性和状态,

  request:在一次Http请求中,容器会返回该Bean的同一实例。而对不同的Http请求则会产生新的Bean,而且该bean仅在当前Http Request内有效,当前Http请求结束,该bean实例也将会被销毁。

  session:在一次Http Session中,容器会返回该Bean的同一实例。而对不同的Session请求则会创建新的实例,该bean实例仅在当前Session内有效。

  global Session:在一个全局的Http Session中,容器会返回该Bean的同一个实例,仅在使用portlet context时有效。

 

8、 Spring中设计模式

  单例模式-针对bean的singleton作用域

  代理模式-AOP中体现的动态代理

  工厂模式- BeanFactory用来创建对象的实例

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324972937&siteId=291194637