bean 四种注入方式详解 Spring( 二 )
四、JavaConfig 方式注入pojo层/** * @Author: crush * @Date: 2021-06-17 17:16 * version 1.0 * JavaConfig 配置 */public class Student4 {@Value("wyh4")private String name;@Value("hngy4")private String school;@Overridepublic String toString() {return "Student4{" +"name='" + name + '\'' +", school='" + school + '\'' +'}';}}
JavaConfig 类@Configurationpublic class Student4Config {@Beanpublic Student4 student4(){return new Student4();}}
xml文件 扫描包<?xml version="1.0" encoding="UTF-8"?><beans xmlns="https://www.yf-zs.com/d/file/p/20210716150016133665/20210716150016133666"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="https://www.yf-zs.com/d/file/p/20210716150018133763/20210716150018133764"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.yf-zs.com/d/file/p/20210716150018133765/20210716150018133766.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.crush.config"/></beans>
测试:@Testpublic void student4(){ApplicationContext context = new ClassPathXmlApplicationContext("student4.xml");Student4 student4 = context.getBean("student4", Student4.class);System.out.println(student4);}
五、Service层注入详解
service/** * @Author: crush * @Date: 2021-06-17 17:27 * version 1.0 * xml 配置 */public interface StudentService1 {void test();}
serviceImpl/** * @Author: crush * @Date: 2021-06-17 17:29 * version 1.0 * xml 配置 */public class StudentService1Impl implements StudentService1{@Overridepublic void test() {System.out.println("===StudentDao1Impl===");}}
xml配置文件<?xml version="1.0" encoding="UTF-8"?><beans xmlns="https://www.yf-zs.com/d/file/p/20210716150016133665/20210716150016133666"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.yf-zs.com/d/file/p/20210716150018133765/20210716150018133766.xsd"><bean id="studentService1" class="com.crush.dao.StudentService1" /></beans>
总结本篇文章就到这里了,希望能给你带来帮助,也希望能够您能够关注趣讯吧的更多内容!
- 四种基本人格类型特征
- 密西西比河最后注入什么湾
- 果啤有酒精吗
- 清理狗狗身上跳蚤的四种方法
- 专门调查有哪四种形式
- 注入洞庭湖的沅江发源地在
- 如何判断兰花该浇水了
- 什么ETF可以当天买卖
- 黑白条纹短裙配什么上衣
- 心理状态包括哪四种
