Spring 7个常用的注解注入

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都有自己的分层角色。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值