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);
}
}