一、概述
Spring是一种轻量级企业应用开发框架,提供了控制反转(IoC)和面向切面编程(AOP)技术。
二、控制反转
控制反转是指程序之间的关系不再直接使用代码进行控制,而是使用容器进行控制。如此控制权就由程序代码转移到外部容器,控制权转移就是控制反转。由于程序组件之间的依赖关系是由容器控制的。在程序运行期间,通过容器动态地将依赖关系注入到组件中就是依赖注入,依赖注入是反转控制的一种形式。在IoC中通过配置文件描述对象之间的依赖关系,而不再使用代码调用其他类对象。在程序运行期间,IoC容器通过加载配置文件,将对象之间的依赖关系注入。
BeanFactory提供了Spring的核心功能,采用工厂模式实现反转控制和依赖注入。通过读取XML文件或属性文件读取JavaBean的定义,从而实现JavaBean的创建、配置和管理。一般将BeanFactory作为容器来获取JavaBean。使用BeanFactory的子接口ApplicationContext读取XML配置文件并加载JavaBean。
//Message.java package bean; public class Message { private String content; public String getContent() { return content; } public void setContent(String content) { this.content = content; } } <!-- beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="bean.Message" name="message"> <property name="content" value="this is a spring test!"/> </bean> </beans> //Solution.java import bean.Message; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Solution { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Message message = (Message) context.getBean("message"); System.out.println(message.getContent()); } }
赋值注入。
//Company.java package bean; public class Company { private int id; private String name; public int getId() { return id; } public String getName() { return name; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } @Override public String toString() { return String.format("[%d: %s]", id, name); } } //Worker.java package bean; import java.util.List; public class Worker { private int id; private String name; private Company company; private List<String> like; public int getId() { return id; } public String getName() { return name; } public Company getCompany() { return company; } public List<String> getLike() { return like; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setCompany(Company company) { this.company = company; } public void setLike(List<String> like) { this.like = like; } @Override public String toString() { return String.format("[%d: %s] works at %s and likes %s", id, name, company, like); } } //spring-config.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="comp" class="bean.Company"> <property name="id" value="101"/> <property name="name" value="Google"/> </bean> <bean id="worker" class="bean.Worker"> <property name="id" value="1001"/> <property name="name" value="Lee"/> <property name="company" ref="comp"/> <property name="like"> <list> <value>Coding</value> <value>Game</value> <value>Basketball</value> </list> </property> </bean> </beans> //Solution.java import bean.Worker; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Solution { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); Worker worker = (Worker) context.getBean("worker"); System.out.println(worker.toString()); } }
构造器注入。
//Company.java package bean; public class Company { private int id; private String name; public Company(int id, String name) { this.id = id; this.name = name; } @Override public String toString() { return String.format("[%d: %s]", id, name); } } //Worker.java package bean; import java.util.List; public class Worker { private int id; private String name; private Company company; private List<String> like; public Worker(int id, String name, Company company, List<String> like) { this.id = id; this.name = name; this.company = company; this.like = like; } @Override public String toString() { return String.format("[%d: %s] works at %s and likes %s", id, name, company, like); } } //spring-config.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="comp" class="bean.Company"> <constructor-arg index="0" value="101"/> <constructor-arg index="1" value="Google"/> </bean> <bean id="worker" class="bean.Worker"> <constructor-arg index="0" value="1001"/> <constructor-arg index="1" value="Lee"/> <constructor-arg index="2" ref="comp"/> <constructor-arg index="3"> <list> <value>Coding</value> <value>Game</value> <value>Basketball</value> </list> </constructor-arg> </bean> </beans> //Solution.java import bean.Worker; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Solution { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); Worker worker = (Worker) context.getBean("worker"); System.out.println(worker.toString()); } }
原文地址:https://www.cnblogs.com/arseneyao/p/9408691.html
时间: 2024-10-03 23:04:03