Spring使用Spring6的JakartaEE的Resource注解完成注入-----Spring框架

package cn.powernode.service;

import cn.powernode.dao.StudentDao;
import cn.powernode.dao.impl.StudentDaoImplForMySQL;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service("studentService")
public class StudentService
{
//    @Resource(name = "studentDaoImplForMySQL")
    private StudentDao studentDao;
//    @Resource(name = "studentDaoImplForMySQL")
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }
    //不能出现在构造方法上,只能出现在类上方法上属性上
//    @Resource(name = "studentDaoImplForMySQL")
    public StudentService(StudentDao studentDao) {
        this.studentDao = studentDao;
    }

    public void deleteStudent()
    {
        studentDao.deleteById();
    }
}

package cn.powernode.service;

import cn.powernode.dao.StudentDao;
import cn.powernode.dao.impl.StudentDaoImplForMySQL;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service("studentService")
public class StudentService
{
// @Resource(name = "studentDaoImplForMySQL")
private StudentDao studentDao;
// @Resource(name = "studentDaoImplForMySQL")
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
//不能出现在构造方法上,只能出现在类上方法上属性上
// @Resource(name = "studentDaoImplForMySQL")
public StudentService(StudentDao studentDao) {
this.studentDao = studentDao;
}

public void deleteStudent()
{
studentDao.deleteById();
}
}
 

<?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">
    <context:component-scan base-package="cn.powernode.dao.impl,cn.powernode.service" use-default-filters="true">

    </context:component-scan>
</beans>

<?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">
<context:component-scan base-package="cn.powernode.dao.impl,cn.powernode.service" use-default-filters="true">

</context:component-scan>
</beans>

package cn.powernode.dao.impl;

import cn.powernode.dao.StudentDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

@Repository("studentDaoImplForOracle")
public class StudentDaoImplForOracle implements StudentDao
{
    private static final Logger logger = LoggerFactory.getLogger(StudentDaoImplForOracle.class);
    @Override
    public void deleteById()
    {
        logger.info("Oracle数据库正在删除学生信息");
    }
}

package cn.powernode.dao.impl;

import cn.powernode.dao.StudentDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

@Repository("studentDaoImplForOracle")
public class StudentDaoImplForOracle implements StudentDao
{
private static final Logger logger = LoggerFactory.getLogger(StudentDaoImplForOracle.class);
@Override
public void deleteById()
{
logger.info("Oracle数据库正在删除学生信息");
}
}

package cn.powernode.dao.impl;

import cn.powernode.dao.StudentDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

//@Repository("studentDao")
public class StudentDaoImplForMySQL implements StudentDao
{
    private static final Logger logger = LoggerFactory.getLogger(StudentDaoImplForMySQL.class);
    @Override
    public void deleteById()
    {
        logger.info("MySQL数据库正在删除学生信息");
    }
}

package cn.powernode.dao.impl;

import cn.powernode.dao.StudentDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

//@Repository("studentDao")
public class StudentDaoImplForMySQL implements StudentDao
{
private static final Logger logger = LoggerFactory.getLogger(StudentDaoImplForMySQL.class);
@Override
public void deleteById()
{
logger.info("MySQL数据库正在删除学生信息");
}
}

package cn.powernode.dao;

import org.springframework.stereotype.Repository;

@Repository("studentDao")
public interface StudentDao
{
    void deleteById();
}

package cn.powernode.dao;

import org.springframework.stereotype.Repository;

@Repository("studentDao")
public interface StudentDao
{
void deleteById();
}

package com.powernode.spring6.test;

import cn.powernode.service.StudentService;
import com.powernode.spring6.bean1.MyDataSource;
import com.powernode.spring6.bean1.User;
import org.junit.Test;
import org.powernode.service.OrderService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAnnotation
{
    @Test
    public void TestResource()
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-resource.xml");
        StudentService studentService = applicationContext.getBean("studentService", StudentService.class);
        studentService.deleteStudent();
    }
    @Test
    public void TestAutoWired()
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-AutoWired.xml");
        OrderService orderService = applicationContext.getBean("orderService", OrderService.class);
        orderService.generate();
    }
    @Test
    public void TestAnnotation()
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
    }
    @Test
    public void TestDIByAnnotation()
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-di.xml");
        MyDataSource myDataSource = applicationContext.getBean("myDataSource", MyDataSource.class);
        System.out.println(myDataSource);
        User user = applicationContext.getBean("user", User.class);
        System.out.println(user);
    }
}

package com.powernode.spring6.test;

import cn.powernode.service.StudentService;
import com.powernode.spring6.bean1.MyDataSource;
import com.powernode.spring6.bean1.User;
import org.junit.Test;
import org.powernode.service.OrderService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAnnotation
{
@Test
public void TestResource()
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-resource.xml");
StudentService studentService = applicationContext.getBean("studentService", StudentService.class);
studentService.deleteStudent();
}
@Test
public void TestAutoWired()
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-AutoWired.xml");
OrderService orderService = applicationContext.getBean("orderService", OrderService.class);
orderService.generate();
}
@Test
public void TestAnnotation()
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
}
@Test
public void TestDIByAnnotation()
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-di.xml");
MyDataSource myDataSource = applicationContext.getBean("myDataSource", MyDataSource.class);
System.out.println(myDataSource);
User user = applicationContext.getBean("user", User.class);
System.out.println(user);
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用注解进行注入,需要满足以下两个条件: 1. 依赖注入的类需要被 Spring 管理,也就是使用 `@Component` 或者其它注解进行标记。 2. 依赖注入的属性需要使用 `@Autowired` 或者其它注解进行标记。 举个例子,我们有一个 Service 类,需要注入一个 DAO 对象: ```java @Component public class UserService { @Autowired private UserDao userDao; // ... } ``` 在上述代码中,`UserService` 类使用了 `@Component` 注解进行标记,表示需要被 Spring 管理。同时,`userDao` 属性使用了 `@Autowired` 注解进行标记,表示需要进行自动注入。 如果 DAO 对象也是一个 Spring 管理的类,则可以直接使用 `@Autowired` 注解进行注入: ```java @Component public class UserDaoImpl implements UserDao { // ... } ``` 如果 DAO 对象不是一个 Spring 管理的类,则需要使用 `@Bean` 注解进行标记,并将其添加到 Spring 容器中: ```java @Configuration public class AppConfig { @Bean public UserDao userDao() { return new UserDaoImpl(); } // ... } ``` 在上述代码中,`AppConfig` 类使用了 `@Configuration` 注解进行标记,表示这是一个 Spring 的配置类。同时,`userDao()` 方法使用了 `@Bean` 注解进行标记,表示需要将其返回值添加到 Spring 容器中,这样 `UserService` 类中的 `@Autowired` 注解就可以自动注入 `userDao` 属性了。 需要注意的是,使用注解进行注入时,Spring 会根据属性类型进行自动匹配,如果匹配到多个 Bean,则需要使用 `@Qualifier` 注解进行限定。同时,也可以使用 `@Resource` 注解或者 `@Inject` 注解进行注入

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旧约Alatus

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值