A brief introduction to spring's IOC and DI xml and annotations

xml

One purpose: Get the object of the required class through the getBean method of the ApplicationContext object.

Write a service class

public class service {
    private String name;public void add(){
        System.out.println("add ....");
    }
}

Write applicationContext.xml file

<bean id="service" class="spring_xml_aop_start.service" />

write test class

public class test {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");//Getting started, later listener loading
        service  us=(service) context.getBean("service");
        us.add();
    }
}

Test class running result

Two purposes Simple attribute injection based on purpose one

There are two forms of property injection: constructor injection and set method injection

Constructor injection: <constructor-arg name="member variable name in class" value="attribute assignment"></constructor-arg> under the corresponding <bean> tag in applicationContext.xml

The class needs to provide a parameterized constructor

Simple Value Injection

When configuring constructor-arg in the corresponding bean in
XML The order of xx)
can also be manually sorted by index="0/1/2" or type="type" or
can be configured by name="class member name" value=""

Complex injection referencing another class

In the class private dao dao; and construct this.dao=dao with parameters;

set injection: <property name="class member name" value="property assignment"></property>

Just provide the set method to the member variable in the class

service class

public class service {
    private String name;
    private dao dao;
    public service(String name,dao dao) {
        super();
        this.name = name;
        this.dao = dao;
    }
    public void add(){
        dao.daotest();
        System.out.println("add ...."+name);
    }
}
dao class
public class dao {
    private String name;
    public void setDao(String dao) {
        this.name = dao;
    }
    public void daotest(){
        System.out.println( "Complex injection" + name);
    }
}

 configuration file

<bean id="dao" class="spring_xml_aop_start.dao">
    <property name="dao" value="我是dao"></property>
</bean> 
<bean id="service" class="spring_xml_aop_start.service">
    <constructor-arg name="name" value="我是service"></constructor-arg>
    <constructor-arg name="dao" ref="dao"></constructor-arg>
</bean> 

Test like above

operation result

 

 annotation

  The first is to configure in applicationContext.xml

​<   context:component-scan base-package="base package" />   //scanned base package

  Then hand over the class to spring management and all four of them can be ("name") equivalent to <bean id="" or name=""> in xml

​ @Component: Components---all classes can use @Controller : Decorate web layer classes @Service : Decorate business layer classes @Repository : Decorate persistence layer classes

  Property injection :

​ Ordinary injection: @Value("value")

  ​ Object type attribute: @Resource(name="name of the above class annotation") can be thankless

​ @Autowired is injected by type annotation combined with @Qualifier by name

  Test class: add the following two paragraphs of comments before the class name

  ​ @RunWith(SpringJUnit4ClassRunner.class)

  ​ @ContextConfiguration(locations="classpath:applicationContext.xml")

  Declare the class to be tested

  @Resource

​   private Service service;

  Add the @Test annotation to the test method ctrl+1 to guide the package 

!!Annotation Notes:

When using annotations to initialize a class, there must be no parameterized construction in the class, otherwise an uninitialized exception will be reported!

Because spring calls the constructor first when initializing the bean, and then sets other properties.

service class

@Service("service")
public class service {
    @Value("service")
    private String name;
    @Resource(name = "daoss" )
     private dao dao;
     // In the previous xml configuration, the constructor was called to inject attributes, and the annotation operation should be removed 
    /* public service(String name, spring_anno_start.dao dao) {
        super();
        this.name = name;
        this.dao = dao;
    }*/
    public void add(){
        dao.daotest();
        System.out.println("add ...."+name);
    }
}

dao class

@Repository("daoss")
public class dao {
    @Value("dao")
    private String name;
    public void daotest(){
        System.out.println( "Complex injection" + name);
    }
}

test class

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class test {
    @Resource
    private service service;
    
    @Test
    public void test1(){
        service.add();
    }
}

applicationContext.xml

<context:component-scan base-package="spring_anno_start"></context:component-scan>

operation result

 

Guess you like

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