标签:creat classpath value pos sep nod 注解 esc tor
配置文件
<?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="day503FactoryBean"></context:component-scan> </beans>
也可以通过配置类来代替
/** * @author wuyimin * @create 2021-05-03-22:21 * @description 配置类 */ @Configuration//作为配置类,代替xml配置文件 @ComponentScan(basePackages = {"day503FactoryBean"}) public class SpringConfig { }
dao daoimpl service
public interface AnnoDao { public void add(); } @Repository(value="DaoImpl1") public class AnnoDaoImpl implements AnnoDao { @Override public void add() { System.out.println("add implements....."); } } @Repository(value="service") public class AnnoService { //定义dao属性 //不需要添加set方法 //添加注入属性的注解 @Autowired//根据类型进行注入 @Qualifier(value = "DaoImpl1")//找到一个实现类 private AnnoDao dao;//接口 @Value(value = "abc") private String name; public void add(){ System.out.println("service add..."); dao.add(); } public void show(){ System.out.println(name); } }
测试类:
public class annotationTest { //使用注解创建对象 @Test public void test01(){ ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("503bean7.xml"); MyBean myBean=context.getBean("thisBean",MyBean.class); myBean.test(); } //使用注解注入属性 @Test public void test02(){ ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("503bean7.xml"); AnnoService myBean=context.getBean("service",AnnoService.class); myBean.add(); myBean.show(); } //完全注解--springboot @Test public void test03(){ ApplicationContext context=new AnnotationConfigApplicationContext(SpringConfig.class); AnnoService myBean=context.getBean("service",AnnoService.class); myBean.add(); myBean.show(); } }
标签:creat classpath value pos sep nod 注解 esc tor
原文地址:https://www.cnblogs.com/wuyimin/p/14884980.html