1.@Component
1.1定义
1.2示例
(1)创建maven工程
(2)pom.xml注入依赖
其他版本也可以,没有特定的版本。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
(3)创建实体类并加入注解
package cn.kgc.entity;
import org.springframework.stereotype.Component;
@Component(value = "user")
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(4)在配置文件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"
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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--声明组件扫描器-->
<context:component-scan base-package="cn.kgc.entity"></context:component-scan>
</beans>
(5)测试类
package cn.kgc.test;
import cn.kgc.entity.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestUser {
@Test
public void test01(){
ClassPathXmlApplicationContext ca = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) ca.getBean("user");
System.out.println(user);
}
}
(6)拓展
注解的value还可以用以下方式来写,省略value="",或者省略(),不过建议还是按照上面的方式写上完整value。
第一种拓展:@Component(“myUser”)
第二种拓展:@Component
2.@Repository
2.1定义
用在持久层上面,方法在dao的实现类上面,表示创建dao对象。
在声明XXMapper的bean对象的时候,@Respository中的value不建议省略。
2.2示例
(1)接口UserMapper.java
package cn.kgc.mapper;
public interface UserMapper {
public void addUser();
}
(2)实现类UserMapperImpl.java
package cn.kgc.mapper;
import org.springframework.stereotype.Repository;
@Repository(value = "userMapper")
public class UserMapperImpl implements UserMapper{
@Override
public void addUser() {
System.out.println("UserMapper的addUser()方法");
}
}
(3)配置文件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"
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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--声明组件扫描器-->
<context:component-scan base-package="cn.kgc.mapper"></context:component-scan>
</beans>
(4)测试类
@Test
public void test04(){
ClassPathXmlApplicationContext ca = new ClassPathXmlApplicationContext("applicationContext.xml");
UserController userController = (UserController) ca.getBean("userController");
userController.addUser();
}
3.@Service
3.1定义
用在业务层上面,放在service的实现类上面,表示创建service对象,可以有一些事务功能。
3.2示例
(1)接口UserService
package cn.kgc.service;
public interface UserService {
public void addService();
}
(2)实现类UserServiceImpl
package cn.kgc.service;
import org.springframework.stereotype.Service;
@Service(value = "userService")
public class UserServiceImpl implements UserService{
@Override
public void addService() {
System.out.println("调用UserService add方法");
}
}
(3)配置文件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"
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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--声明组件扫描器-->
<context:component-scan base-package="cn.kgc.service"></context:component-scan>
</beans>
(4)测试类
@Test
public void test03(){
ClassPathXmlApplicationContext ca = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) ca.getBean("userService");
userService.addService();
}
4.@Controller
4.1定义
4.2示例
(1)创建类UserController
package cn.kgc.controller;
import org.springframework.stereotype.Controller;
@Controller(value = "userController")
public class UserController {
public void addUser(){
System.out.println("控制器添加方法");
}
}
(2)配置文件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"
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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--声明组件扫描器-->
<context:component-scan base-package="cn.kgc.controller"></context:component-scan>
</beans>
(3)测试类
@Test
public void test04(){
ClassPathXmlApplicationContext ca = new ClassPathXmlApplicationContext("applicationContext.xml");
UserController userController = (UserController) ca.getBean("userController");
userController.addUser();
}
5.@Value
5.1定义
给简单类型属性对象赋值。
5.2示例
(1)创建maven工程
(2)实体类加入注解
package cn.kgc.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component(value = "user")
public class User {
@Value(value = "1")
private Integer id;
@Value(value = "张三")
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(3)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"
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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--声明组件扫描器-->
<context:component-scan base-package="cn.kgc.entity"></context:component-scan>
</beans>
(4)测试类
@Test
public void test01(){
ClassPathXmlApplicationContext ca = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) ca.getBean("user");
System.out.println(user.getId()+user.getName());
}
6.@Autowired
给引用类型属性赋值(自动装配Bean),默认使用byType自动注入。
6.1定义
6.2示例
(1)创建maven工程
(2)实体类User
package cn.kgc.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component(value = "user")
public class User {
@Value(value = "1")
private Integer id;
@Value(value = "张三")
private String name;
@Resource
private Role role;
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(3)实体类Role
package cn.kgc.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component(value = "role")
public class Role {
@Value(value = "1")
private Integer rId;
@Value(value = "老师")
private String rName;
public Integer getrId() {
return rId;
}
public void setrId(Integer rId) {
this.rId = rId;
}
public String getrName() {
return rName;
}
public void setrName(String rName) {
this.rName = rName;
}
}
(4)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"
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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--声明组件扫描器-->
<context:component-scan base-package="cn.kgc.entity"/>
</beans>
(5)测试类
package cn.kgc.test;
import cn.kgc.entity.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
@Test
public void testdemo001(){
String conf = "applicationContext.xml";
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf);
User myUser = (User) ac.getBean("user");
System.out.println(myUser.getId()+" "+myUser.getName()+" "+myUser.getRole().getrName()+" "+myUser.getSonRole().getrName());
}
}
7.@Resource
7.1定义
给引用类型赋值,Spring提供了对JDK注解@Resource的支持,默认按名称注入,如果按名称注入失败,自动按类型注入。
7.2示例
(1)创建maven工程
(2)注入依赖pom.xml
(3)实体类User
package cn.kgc.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component(value = "user")
public class User {
@Value(value = "1")
private Integer id;
@Value(value = "张三")
private String name;
@Resource
private Role role;
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(4)实体类Role
package cn.kgc.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component(value = "role")
public class Role {
@Value(value = "1")
private Integer rId;
@Value(value = "老师")
private String rName;
public Integer getrId() {
return rId;
}
public void setrId(Integer rId) {
this.rId = rId;
}
public String getrName() {
return rName;
}
public void setrName(String rName) {
this.rName = rName;
}
}
(5)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"
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 https://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="cn.kgc.entity"></context:component-scan>
</beans>
(6)测试类
package cn.kgc.test;
import cn.kgc.entity.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
@Test
public void test01(){
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) ac.getBean("user");
System.out.println(user.getName()+user.getRole().getrName());
}
}
8.拓展
@Component和@Repository,@Service,@Controller的异同
同:都可以创建对象
异:@Component,@Repository,@Service,@Controller都有自己的分层角色。