IOC Annotation Development in Spring

Initial Exploration of IOC Annotation Development

First, after spring 4, to use annotation form, you must introduce aop packages and a context constraint in the configuration file

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

Configuration Annotation Scanning, which type of package is scanned by Annotation

<context:component-scan base-package="pers.liuchengyin.demo1"/>

3. Adding annotations to classes and injecting attributes with annotations

(1) No set method is provided. Add @value("value") directly to the attribute name.

@Component("user")
public class User {
    @Value("Liu Chengyin")
    public String name;
}

(2) Provide a set method by adding @value("value") to the set method

@Component("user")
public class User {
    public String name;

    @Value("Liu Chengyin")
    public void setName(String name) {
        this.name = name;
    }
}

Use of annotations

I. Class Annotations

@Component

Modifying a class to give it to Spring management is equivalent to configuring <bean id=""class="> in the configuration file.

@ Three Derivative Annotations of Component

For better layering, Spring can use three other annotations with similar functionality

@ Controller web Layer

@ service Layer

@ Repository dao layer

II. Attribute Injection

@ Value

@Value("Liu Chengyin")
public void setName(String name) {
    this.name = name;
}

@ Autowire sets the property value of the object type

In this way, property injection is done completely by type, without the need to use id names on annotations.

// This id doesn't work.
@Component("dog")
public class Dog {
    @Value("Siberian Husky")
    public String name;
}	
@Controller("user")
public class User {
    public String name;
    // Direct injection
    @Autowired
    public Dog dog;

    @Value("Liu Chengyin")
    public void setName(String name) {
        this.name = name;
    }
}		

Traditionally, property injection is done by name, and the @Autowire annotation must be used with @Qualifier

@Autowired       // According to type
@Qualifier("dog")    // By name
public Dog dog;	

You can use @Reource alone instead.

@Resource(name="dog")
public Dog dog;

Initialization method with @PostConstruct tag and @PreDestroy tag destruction method

@PostConstruct
public void init(){
    System.out.println("User Initialization");
}

@PreDestroy
public void destroy(){
    System.out.println("User Destruction");
}

Use @scope to mark scope

 

Comparisons of XML and Annotation

XML can be used in any scenario with clear structure and easy maintenance.

Annotations are not classes provided by themselves that can not be used, and the development is simple and convenient.

Integrated Development of XML and Annotations

Let XML manage beans and annotations complete attribute injection

In the process of use, no scanning is needed. Scanning is to use attribute annotation @Resource@Value@Autowired@Qulifier for annotations on classes without scanning.

<context:annotation-config/>

Examples:

<context:annotation-config/>
<bean id="person" class="pers.liuchengyin.demo1.Person"></bean>
public class Person {
    @Value("Liu Chengyin")
    public String name;
}
public void test(){
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    Person person = (Person) applicationContext.getBean("person");
    System.out.println(person.name);
}

 

 

Keywords: xml Spring Attribute

Added by Boxerman on Mon, 19 Aug 2019 09:46:36 +0300