2.Spring Getting Case

2.Spring Getting Case
Initial Configuration
1. Import Jar package
Write code
Upgrading a case
IOC ways to create objects
Created by no constructor parameters
There is created by arg constructor
 
 

Spring configuration (beans.xml)
Initial Configuration
1. Import Jar package
Note: spring need to import commons-logging logging We use maven, he will automatically download the corresponding dependencies.
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.1.10.RELEASE</version>
</dependency>

 Write code

1. Write a class Hello entity
public class Hello {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void show(){
        System.out.println("Hello,"+ name );
    }
}
 2, preparation of Spring file, here named beans.xml
<?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">
 
    <-! Bean is java objects, created and managed by the Spring -> 
    < bean the above mentioned id = "the Hello" class = "com.kuang.pojo.Hello" > 
        < Property name = "name" value = "the Spring" /> 
    </ the bean > // here is equivalent to a new Object
 </ Beans >

 3. Test

@Test
 public  void Test () {
     // parse beans.xml file, generates a corresponding management Bean object 
    the ApplicationContext context = new new the ClassPathXmlApplicationContext ( "beans.xml" );
     // the getBean: Spring configuration file parameter is the id of the bean. 
    Hello = hello (hello) context.getBean ( "Hello"); // the getBean = obtaining target 
    hello.show ();
}
 

 Here, will find that the whole process, we do not need a new object can be generated directly through the proxy Spring, Hello object attributes provided by the spring containers, called the teach process control inversion.

  • Control: Who will control the creation of an object, the object of legacy applications is controlled by the program itself was created after using Spring, the object is to create the Spring
  • Reverse: The program itself does not create objects, but instead passively receiving object.
Dependency injection: the method is performed using a set of injection.
IOC is a programming idea, changed from passive receiving active program
Upgrading a case
In the previous case above, add a Spring configuration file beans.xml
<?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">
 
    <bean id="MysqlImpl" class="com.kuang.dao.impl.UserDaoMySqlImpl"/>
    <bean id="OracleImpl" class="com.kuang.dao.impl.UserDaoOracleImpl"/>
 
    < Bean the above mentioned id = "ServiceImpl" class = "com.kuang.service.impl.UserServiceImpl" > 
        <-! Note: name attribute is not here, but that part of the back of the set method,
         Lowercase first letter -> 
        <! - a reference to another bean, not by value but with the ref -> 
        < Property name = "userDao" ref = "OracleImpl" /> 
    </ bean >
 
</beans>

 

test
@Test
public void test2(){
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    UserServiceImpl serviceImpl = (UserServiceImpl) context.getBean("ServiceImpl");
    serviceImpl.getUser();
}

 

OK, now, you do not have to completely change the program to go to achieve different operations, only need to make changes in the xml configuration file, called IoC, get word: the object is created by Spring, management, assembly!

IOC ways to create objects
        Created by no constructor parameters
1.User.java constructor with no arguments can Set to implantation method
public class User {
    private String name;
    public User() {
        System.out.println ( "User constructor with no arguments" );
    }
    public void setName(String name) {
        this.name = name;
    }
    public void show(){
        System.out.println("name="+ name );
    }
}

 

2.beans.xml
<?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">
 
    <bean id="user" class="com.kuang.pojo.User">
        <property name="name" value="kuangshen"/>
    </bean>
 
</beans>

3. Test

@Test
public void test(){
    Context ApplicationContext = new new ClassPathXmlApplicationContext ( "beans.xml" );
     // in the implementation of getBean, user has been created by no-argument constructor 
    the User the User = (the User) context.getBean ( "the User" );
     // call the object . method 
    user.show ();
}

 

      There is created by arg constructor
1.User2.java
public class UserT {
    private String name;
    public UserT(String name) {
        this.name = name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public void show(){
        System.out.println("name="+ name );
    }
}

 

Three ways to write 2.beans.xml
<! - a first subscript index set according to the parameters -> 
< the bean ID = "userT" class = "com.kuang.pojo.UserT" > 
    <! - index refers to the construction method, the start index may be from 0 direct injection into the constructor, the following labels -> 
    < constructor Arg- index = "0" value = "kuangshen2" /> 
</ the bean >
 
<! - The second parameter is set according to the name -> 
< bean the above mentioned id = "userT" class = "com.kuang.pojo.UserT" > 
    <! - name refers to the parameter name parameter name, specify the constructor - > 
    < constructor Arg- name = "name" value = "kuangshen2" /> 
</ the bean >

 
<! - A third set according to the parameter type -> 
< the bean ID = "userT" class = "com.kuang.pojo.UserT" > 
    <! - set by the parameter type, String all types are provided, not recommended -> 
    < constructor Arg- type = "java.lang.String" value = "kuangshen2" /> 
</ the bean

Conclusion: In the configuration file load time. Which managed objects are initialized!

Spring configuration (beans.xml)
Aliases
alias set the alias, the alias of the bean is provided, you may be provided a plurality of aliases
<! - Set Alias: Bean can be used at the time of acquisition of Alias acquisition -> 
< Alias name = "userT" Alias = "UserNew" />

Bean Configuration

<-! Bean is java objects, created and managed by the Spring ->
 
<!--
    bean id is an identifier unique to, if not configured id, name is the default identifier
    If you configure id, and configure the name, then the name is an alias
    may be provided a plurality of name aliases, as a comma, semicolon separated, space
    If no id and name, the object can be acquired in accordance with applicationContext.getBean (.class);
 
    bean class is the fully qualified name = name + class package name
-->
<bean id="hello" name="hello2 h2,h3;h4" class="com.kuang.pojo.Hello">
    <property name="name" value="Spring"/>
</bean>

import

<import resource="{path}/beans.xml"/>
Import other Beans . Xml
 

Guess you like

Origin www.cnblogs.com/blogger-Li/p/12298747.html