Spring --- Getting Started

Spring: is a layered one-stop lightweight open-source framework. ORM framework Hibernate- persistence layer, MVC framework Struts2-Web layer

One-stop framework: EE has developed solutions for each layer
      Web layer: SpringMVC
      Service layer: Spring's Bean management, Spring's declarative transaction
      Dao layer: Spring's JDBC templates, Spring's ORM module

1. preliminary understanding IOC (Inversion of Control)

       JavaWeb new project, import the jar package
       spring development environment Download https://repo.spring.io/release/org/springframework/spring/   
       download spring-framework-4.2.3.RELEASE-dist.zip and extract
       

       Import the following jar package
       

       UserDao his new interface and implementation class UserDaoImpl just write method, as long as the console output something on the line. Then create a new file in the src directory applicationContext.xml, the introduction of constraint, xsd-configuration.html \ under spring-framework-4.2.4.RELEASE-dist in the development environment of docs \ spring-framework-reference \ html page in the most bottom, there is an example

       After the introduction of constraints, the configuration of the bean UserDao
       

       Write test classes
       

2.IOC 和 OF

       IOC:控制反转,将对象的创建权反转给Spring
       DI:依赖注入,前提是必须有IOC环境,配置了相应的bean。

       当Spring在帮我们创建一个类的示例的时候,发现这个类需要某个属性,他会自动将属性设置到类里面,例如当UserDaoImpl中有一个属性username的时候,传统方式UserDao userDao=new UserDaoImpl(),并不能调用setUsername为其设置name,因为UserDao这个接口中没有这个方法,就无法设置username属性。当将类的创建教给Spring后,他会在创建的时候将该类需要的属性也一并注入到类中(需要给bean配置property标签),这就叫依赖注入(DI),此时输出的值是有username的

3.Spring的工厂类

    3_1.BeanFactory(老版本)

        BeanFactory---会在调用getBean的时候实例化Spring管理的类

    3_2.ApplicationContext(新版本)

        ApplicationContext---会在加载配置文件的时候实例化Spring管理的类
        ApplicationContext有两个实现类
             ClassPathXmlApplicationContext.java   :加载类路径下的配置文件(src路径下的配置文件)
             FileSystemXmlApplicationContext.java  :加载文件系统下的配置文件(项目之外,如本地磁盘上的配置文件)

4.Spring的配置

     属性注入的方式:

           普通注入:

           使用p名称空间注入:

           使用SpEL属性注入:注入普通属性时字符串需要加单引号,#{}中可以使用其他类的方法或属性,  比 如#{car2.name} , #{car2.getName()}

      

           注入List和Map

      

5.Spring的IOC注解方式

   入门

       1.在原来的基础上引入spring-aop-4.2.4.RELEASE.jar(一共5个Spring的包,两个日志文件的包)

       2.在src下的applicationContext.xml下引入context的约束,上面引入了bean的约束,bean的约束是最基本的约束,引入方式和上面一样,在html中找context的约束。

       3.在xml中配置要使用注解的包,包下的类就可以使用注解了
        

       4.添加注解

       5.注解方式的属性注入,这种方式不需要提供set方法,只需要在要注入的属性上面加入@Value("要注入的值"),如果有set方法就在set方法上面加上该注解

   注解详情

       1.@Component("id"):组件
            *_修饰一个类,将一个类交给Spring管理
            *_有三个衍生注解(目前功能和Component是一样的)
                   *Controller     :修饰web层
                   *Service         :修饰Service层
                   *Repository    :修饰Dao层

       2. The property annotations injected:
            * @Value ( "value"): Set the value of common attributes
            * @Autowried: set the value of the attribute of the object type, in accordance with the type of injection to complete the property (id Component that is disposed in this annotation at useless, he will find a corresponding interface implementation class when implanting)
            * @Resource (name = "the above mentioned id"): setting object type attribute, complete injection by name, id values must be objects you need Component notes id of the same value

6. Using XML and annotations Integrated Development (XML managed bean, annotation attributes to achieve injection)

       Note that adding <context: annotation-config /> configuration, the original <context: component-scan base- package = "xx.xxx" /> configuration may be deleted, these two latter configuration functions including functions of the former, the difference the former is generally used to activate those bean has been registered in the spring container, which not only can be activated, you can also register bean. For details, please refer to this blog: https://www.cnblogs.com/leiOOlei/p/3713989.html

Guess you like

Origin www.cnblogs.com/zy-Luo/p/11033214.html