Spring使用注解

在Spring中,尽管使用XML配置文件可以实现Bean的装配工作,但是如果应用中有许多Bean时,会导致XML配置文件过于臃肿,给后续的维护和升级工作带来一定的困难。为此Spring提供了对Annotation(注解)技术的全面支持。

常用的注解如下:

@Component:可以使用此注解描述Spring中的Bean,但它是一个泛化的概念仅仅表示一个组件,并且可以作用任何层次。使用时只需将其标注在相应的类上即可。

@Repository:用于将数据访问层(Dao层)的类标识为Spring中的Bean,作用与Component相同。

@Service:通常作用在业务逻辑层(Service层),作用与Component相同。

@Controller:通常作用在控制层(如Spring MVC的Controller),用于将控制层的类标识为Bean,作用与Component相同。

@Autowired:用于对Bean的属性变量、属性的setter方法及构造方法进行标注,配合对应的注解处理器完成Bean的自动配置工作。默认按照Bean的类型进行装配。

@Resource:其作用与@Autowired一样。其区别在于@Autowired默认按照Bean类型装配,而@Resource默认按照Bean实例名称进行装配。@Resource有两个重要属性:name和type。Sping将name解析为Bean实例名称,type解析为Bean实例类型。如果指定name属性,则按照实例名称进行装配;如果指定type属性,就按Bean类型进行装配;如果都不指定,就先按Bean实例名称进行装配,如果不能匹配,再按照Bean类型进行装配;如果都不能匹配,就抛出NoSuchBeanDefinitionException异常。

@Qualifier:与@Autowired注解配合使用,会将默认的按Bean类型装配修改为按Bean的实例名称进行装配,Bean的实例名称由@Qualifier注解的参数指定。

在上面的几个注解中,虽然@Repository、@Service与@Controller功能与@Component功能相同,但为了标注类用途更加清晰,在实际开发中,最好使用@Repository、@Service与@Controller进行标注。

1.首先,导入jar包。spring-aop-4.3.6.RELEASE.jar

2.写一个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-4.3.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 
<!-- 	使用context命名空间,通知Spring扫描指定包下所以Bean类,进行注释解析 -->
	<context:component-scan base-package="services" />
	<context:component-scan base-package="dao" />
	<context:component-scan base-package="enties" />
</beans>

3.在Bean类中加注解

如:

package services;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import dao.ProjectDao;
import enties.Project;
@Service("projectservice")
public class ProjectService {
	@Resource(name="projectDao")
	ProjectDao projectDao;
	public void setProjectDao(ProjectDao projectDao) {
		this.projectDao = projectDao;
	}
	public List<Project> getAll() throws SQLException{
		return projectDao.getAll();
	}
	public boolean addProject(Project p) throws SQLException{
		return projectDao.addProject(p);
	}

}
package dao;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Repository;
import enties.Project;
import utils.JdbcUtils;
@Repository("projectDao")
public class ProjectDao {

	public  List<Project> getAll() throws SQLException{
		Connection conn=JdbcUtils.getConnection();
		QueryRunner qr=new QueryRunner();
		String sql="select * from T_project";
		List list=qr.query(conn, sql, new BeanListHandler(Project.class));
		return list;
	}
	public  boolean addProject(Project p) throws SQLException{
		Connection conn=JdbcUtils.getConnection();
		QueryRunner qr=new QueryRunner();
		String sql="insert into T_Project values(?,?,?,?,?)";
		int flag=qr.update(conn, sql, new Object[]{p.getProject_id(),p.getProject_name(),p.getDeputy_name(),p.getTelephone(),p.getAddr()});
		if(flag>0){
			return true;
		}else{
			return false;
		}
	}

猜你喜欢

转载自blog.csdn.net/cc1969281777/article/details/82824184