8.2. Examples of using spring annotations

XML based bean property configuration: the definition information of bean is separated from the implementation class of bean.

Annotation based configuration: the definition information of bean is realized by annotation on the bean implementation class.

That is to say, the annotation is equivalent to the configuration in XML, the same.


1, Configuration before spring start

Configuring in 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" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd"
         >
   <context:component-scan base-package="com"/>
</beans>


be careful:

1: Context: component scan: scan all classes in this package, and get basic bean information from annotation information (no scanning or instantiation without annotation).


2: For scanned components,
Method 1. Use the unqualified class name, the first letter is lowercase (for example: Person class, beanname=person) (default, if there is no value attribute)


Method 2. Use the value attribute to identify the component name



2, Example:

1. Structure catalog


2. Code

Person.java (use the value attribute to identify the component name)

package com;

import org.springframework.stereotype.Component;

@Component(value="person1")
public class Person {
	public void test() {
		System.out.println("User information");
	}
	
}

Test.java

package test;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.Person;

public class Test {

	public static void main(String[] args) {
		ConfigurableApplicationContext bf=new ClassPathXmlApplicationContext("beans.xml");
		Person per=(Person) bf.getBean("person1");
		
		per.test();	
	}
}


Configure 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" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context-3.0.xsd"
         >
   <context:component-scan base-package="com"/>
</beans>


Run Test.java, and it will be displayed in the printer

User information

3, Conclusion:

The annotation @ Component is added to the Person class. When spring sees this attribute flag, it will automatically change the Person into a container management class, which is equivalent to the configuration in XML as follows:

 <bean id="person" class="com"></bean>

Tags: xml Spring Attribute Java

Posted on Sun, 03 May 2020 21:41:17 -0400 by chaotica