Use of Spring annotations

Spring annotations can make full use of Java's reflection mechanism to obtain class structure information, which can effectively reduce configuration work. For example, when using JPA annotations to configure ORM mapping, we do not need to specify the PO attribute name, type and other information. If the relationship table field is consistent with the PO attribute name and type, you don’t even need to write task attribute mapping information, because all of this information can Obtained through the Java reflection mechanism.
The following example is done on the JSP+Servlet+JDBC project.
1. Use the annotation @Repository in the dao layer. It
should be noted that the annotation is written in the implementation class of the interface, not the interface. In order for Spring to scan the classes in the classpath and recognize the @Repository annotation, you need to enable the automatic scanning of beans in the XML configuration file.

package com.gx.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.gx.dao.IBasicsDataDao;
import com.gx.po.User;

//value="basicsDataDao"中的basicsDataDao就是这个接口在spring容
@Repository(value="basicsDataDao")
public class BasicsDataImpl implements IBasicsDataDao {
    
    

	@Override
	public List<User> selectAll() {
    
    
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User findById(int id) {
    
    
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int update(User t) {
    
    
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insert(User t) {
    
    
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteById(int id) {
    
    
		// TODO Auto-generated method stub
		return 0;
	}
}

2. Use the annotation @Service in the service layer:
After the annotation is used, the new method is no longer used to instantiate the dao layer. Inject the dao layer using the annotation @Autowired (it can annotate class member variables, methods and constructors to complete the automatic assembly work), Spring will inject the objects of the dao layer into the userDao variable, here the variable name suggestions and the dao layer annotations The value of is kept consistent. The @Resource annotation can also be used to inject the dao layer. Both @Resource and @Autowired annotations are used to implement dependency injection. Only @Autowried is automatically injected according to byType, while @Resource is automatically injected according to byName by default.

package com.gx.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gx.dao.IBasicsDataDao;
import com.gx.po.User;
import com.gx.service.IBasicsDataService;

@Service(value="basicsDataService")
public class BasicsDataServiceImpl implements IBasicsDataService {
    
    

	//第一种写法:自动装配
	@Autowired
	private IBasicsDataDao basicsDataDaoOne;
	
	//第二种写法
	@Resource
	private IBasicsDataDao basicsDataDaoTwo;
	
	@Override
	public List<User> selectAll() {
    
    
		// TODO Auto-generated method stub
		return basicsDataDaoOne.selectAll();
	}

}

3. Spring is configured in applicationContext.xml as follows:

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.1.xsd 
	http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
	
	<!-- 开启注解 -->
	<context:annotation-config />
	<!-- 配置扫描路径, 去扫描有注解的包名-->
	<context:component-scan base-package="com.gx.dao,com.gx.service,com.gx.aop" />
	
	<!-- 自动为 spring容器中那些配置@Aspect切面的bean创健建代理,放入切面 -->
	<aop:aspectj-autoproxy />
</beans>

Guess you like

Origin blog.csdn.net/weixin_44547592/article/details/108281166