spring注解入门案例
spring注解入门案例
- 基于maven项目
1、准备jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
2、编写实体类
package com.yl.bean;
import java.io.Serializable;
/**
* 用户实体类
*/
public class User implements Serializable {
private Integer id;
private String username;
private String password;
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
3、编写业务层
- 业务层接口
package com.yl.service;
/**
* 用户业务层接口
*/
public interface IUserService {
/**
* 查询用户
*/
public void queryUser();
}
- 业务层实现类
package com.yl.service.impl;
import com.yl.dao.IUserDao;
import com.yl.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* 用户业务层接口实现类
*/
@Service("userService")//使用注解创建业务层bean对象
public class IUserServiceImpl implements IUserService {
@Resource(name = "userDao")//使用注解注入指定bean对象
private IUserDao iUserDao;//用户持久层对象
/**
* 查询用户
*/
@Override
public void queryUser() {
iUserDao.queryUser();
}
}
4、编写持久层
- 持久层接口
package com.yl.dao;
/**
* 用户持久层接口
*/
public interface IUserDao {
/**
* 查询用户
*/
public void queryUser();
}
- 持久层实现类
package com.yl.dao.impl;
import com.yl.dao.IUserDao;
import org.springframework.stereotype.Repository;
/**
* 用户持久层接口实现类
*/
@Repository("userDao")//使用注解创建持久层bean对象
public class IUserDaoImpl implements IUserDao {
/**
* 查询用户
*/
@Override
public void queryUser() {
System.out.println("查询成功");
}
}
5、配置文件
- 配置文件放在资源目录(根目录)下
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--指定spring要扫描的包-->
<context:component-scan base-package="com.yl"></context:component-scan>
</beans>
6、模拟表现层(测试)
package com.yl.ui;
import com.yl.service.IUserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 表现层
*/
public class UI {
public static void main(String[] args) {
//获取spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
//获取业务层对象
IUserService userService= (IUserService) applicationContext.getBean("userService");
//查询用户
userService.queryUser();
}
}
记得快乐