使用注解简化Spring IOC

一、Spring IOC

1、实体类

package com.gaj.entity;

import java.util.Date;

public class UserInfo {
	
	private Integer uid;
	private String uname;
	private Date regDate;
	private Double money;
	
	public Integer getUid() {
		return uid;
	}
	public void setUid(Integer uid) {
		this.uid = uid;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public Date getRegDate() {
		return regDate;
	}
	public void setRegDate(Date regDate) {
		this.regDate = regDate;
	}
	public Double getMoney() {
		return money;
	}
	public void setMoney(Double money) {
		this.money = money;
	}

	@Override
	public String toString() {
		return "UserInfo [uid=" + uid + ", uname=" + uname + ", regDate=" + regDate + ", money=" + money + "]";
	}

}

2、DAO层接口

package com.gaj.dao;

import java.util.List;

import com.gaj.entity.UserInfo;

public interface UserDAO {
	
	public List<UserInfo> findAllUsers() throws Exception;
	
}

3、DAO层实现类

package com.gaj.dao.impl;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.gaj.dao.UserDAO;
import com.gaj.entity.UserInfo;

public class UserDAOImpl implements UserDAO {
	
	private QueryRunner qr;
	
	public void setQr(QueryRunner qr) {
		this.qr = qr;
	}


	@Override
	public List<UserInfo> findAllUsers() throws Exception {
		// 设置返回值
		List<UserInfo> users = null;
		// 编写sql
		String sql = "select u_id uid,u_name uname,u_regdate regDate,u_money money from user_info";
		// 执行sql
		users = qr.query(sql, new BeanListHandler<>(UserInfo.class));
		// 返回
		return users;
	}

}

4、Service层接口

package com.gaj.service;

import java.util.List;

import com.gaj.entity.UserInfo;

public interface UserService {
	
	public List<UserInfo> findAllUsers() throws Exception;
	
}

5、Service层实现类

package com.gaj.service.impl;

import java.util.List;

import com.gaj.dao.UserDAO;
import com.gaj.entity.UserInfo;
import com.gaj.service.UserService;

public class UserServiceImpl implements UserService {
	
	private UserDAO userDAO;
	
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	@Override
	public List<UserInfo> findAllUsers() throws Exception {
		return userDAO.findAllUsers();
	}

}

6、XML配置

applicationContext.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql://127.0.0.1:3306/user_db?characterEncoding=utf-8" />
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>

	<bean id="qr" class="org.apache.commons.dbutils.QueryRunner">
		<constructor-arg name="ds" ref="dataSource" />
	</bean>
	
	<bean id="userDAO" class="com.gaj.dao.impl.UserDAOImpl">
		<property name="qr" ref="qr" />
	</bean>
	
	<bean id="userService" class="com.gaj.service.impl.UserServiceImpl">
		<property name="userDAO" ref="userDAO" />
	</bean>

</beans>

7、测试类

package com.gaj.test;

import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.gaj.entity.UserInfo;
import com.gaj.service.UserService;

public class SpringTest {

	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	
	@Test
	public void query() throws Exception{
		UserService userService = context.getBean("userService", UserService.class);
		List<UserInfo> users = userService.findAllUsers();
		users.forEach(System.out::println);
	}
	
}

二、使用注解优化

1、DAO层实现类

package com.gaj.dao.impl;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.gaj.dao.UserDAO;
import com.gaj.entity.UserInfo;

@Component
public class UserDAOImpl implements UserDAO {
	
	@Autowired
	private QueryRunner qr;

	@Override
	public List<UserInfo> findAllUsers() throws Exception {
		// 设置返回值
		List<UserInfo> users = null;
		// 编写sql
		String sql = "select u_id uid,u_name uname,u_regdate regDate,u_money money from user_info";
		// 执行sql
		users = qr.query(sql, new BeanListHandler<>(UserInfo.class));
		// 返回
		return users;
	}

}

2、Service层实现类

package com.gaj.service.impl;

import java.util.List;

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

import com.gaj.dao.UserDAO;
import com.gaj.entity.UserInfo;
import com.gaj.service.UserService;

@Component
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserDAO userDAO;
	
	@Override
	public List<UserInfo> findAllUsers() throws Exception {
		return userDAO.findAllUsers();
	}

}

3、XML配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd">
	
	<context:component-scan base-package="com.gaj.dao" />
	<context:component-scan base-package="com.gaj.service" />

	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url"
			value="jdbc:mysql://127.0.0.1:3306/user_db?characterEncoding=utf-8" />
		<property name="username" value="root" />
		<property name="password" value="root" />
	</bean>

	<bean id="qr" class="org.apache.commons.dbutils.QueryRunner">
		<constructor-arg name="ds" ref="dataSource" />
	</bean>

</beans>

4、测试类

package com.gaj.test;

import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.gaj.entity.UserInfo;
import com.gaj.service.UserService;

public class SpringTest {

	ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
	
	@Test
	public void query() throws Exception{
		UserService userService = context.getBean(UserService.class);
		List<UserInfo> users = userService.findAllUsers();
		users.forEach(System.out::println);
	}
	
}

三、Spring注解讲解

1、Spring注解

XML的另外一种配置方案,它能够减少编程人员在XML中配置的bean标签的数量,从而简化XML配置。
主要用途:配置bean
1. 创建类

@Component相当于在XML中配置<bean class="" />,class配置的就是该类的全限定名
@Component(“person”)相当于在XML中配置<bean id=“person” class="" />,指定了id,并配置该类的全限定名

@Component注解可以将一个类变成bean,但是语义不够明显,为此@Component注解有3个子注解:@Repository、@Service、@controller。

  • @Repository 用于DAO层,可以把一个DAO变成一个bean
  • @Service 用于Service层,可以把一个service变成一个bean
  • @controller 用于web层,可以把一个controller变成一个bean

附注:如果想要将创建的bean单例模式改成多例模式,在类上方添加@Scope(“prototype”)即可

2. 包扫描器

需要在xml文件中声明引入功能标签

xmlns:context="http://www.springframework.org/schema/context"

添加标签的xsd文件

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

配置包扫描器

	<!-- 表示扫描某个包以及子包 -->
	<context:component-scan base-package="*.*.dao" />
	<context:component-scan base-package="*.*.service" />

3. 注入属性

  • 按类型注入属性值(给属性按类型赋值):@AutoWired
  • 按bean的id注入属性值(给属性按名称赋值):@Qualifier(“id属性名”)

@Autowired注解可以按类型注入相关的属性值,但是按类型注入一个类型只能出现一个bean,假如说你按类型注入的时候报错了,可以按照BeanID的形式指定需要注入的bean

四、Spring注解总结

  1. 接口一定不能配置注解bean,因为接口不能创建对象
  2. DAO中的bean配置注解@Repository
  3. Service的bean配置注解@Service
  4. Web层的bean配置注解@controller
  5. 配置注解不加id相当于配置了一个<bean class=""> 没有配置id,因此在获取这个bean的对象的时候只能按类型获取@Autowired
  6. 当使用@Autowired注解的时候,是可以不用给属性提供set方法的
  7. 注解配置完的bean,散落在了不同的包中,spring需要知道配置了注解bean的位置,因此需要在xml文件中配置包扫描器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值