Spring? Annotation mode injection

This blog series is summarized here: Spring summary

Annotation mode injection

The source project file is: spring3.2

Annotation mode injection

Step 1: introduce dependency package, download , extraction code: cx69.


Step 2: import constraint file

<?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.2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	
</beans>

Step 3: turn on the drive of annotation

<?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.2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<!-- Enable annotation drive -->
	<context:annotation-config/>
	
	<bean id="userDao" class="com.wyx.spring.dao.impl.UserDaoImpl"></bean>
	
	<bean id="userService" class="com.wyx.spring.service.impl.UserServiceImpl"></bean>
</beans>

1. @ Resource comment

Add to attribute

You do not need to provide a Set method to define the properties to inject into a Bean.

public class UserServiceImpl implements UserService 
{
	@Resource
	private UserDao userDao;

	@Override
	public void save()
	 {
		userDao.save();
	}
}

During spring container initialization, the @ Resource annotation first matches the bean id in the spring container according to the name of the @ Resource attribute (the variable name (userDao) is used when no name is specified by default). When the @ Resource name is not specified by default, if the name does not match, it matches according to the relationship between the interface and the implementation class. If an interface exists When there are multiple implementation classes, we must specify the name attribute of @ Resource to specify which bean to inject

If name is specified, it will be injected if it can be found. If it cannot be found, an error will be reported, and matching will not be performed according to the type

@The Resource annotation is not a spring annotation, but a javax annotation.

Add to Set method

Add @ Resource annotation to the Set method. When the name of @ Resource is not specified by default, the bean id can be injected if any of the three boxes on the left can match. If the three names do not match, they will match according to the relationship between the interface and the implementation class. If two implementation classes are encountered, an error will be reported. In this case, you need to specify the name of @ Resource

Once the @ Resource name is specified, if the name does not match, an error will be reported directly, and the matching will not be performed according to the type

2. @ Autowired annotation

Add to attribute

@Autowired is matched according to the relationship between the interface and the implementation class. If there are multiple interface implementation classes, we must specify the name to match and the @ qualifier annotation to specify the value and bean id match to inject.

Add to Set method

If there is any mistake, please correct it!

467 original articles published, 210 praised, 80000 visitors+
His message board follow

Tags: Spring Attribute xml encoding

Posted on Thu, 30 Jan 2020 01:36:20 -0500 by jackdcrack