目录
IOC容器
基于XML的加载方式
package com.bean;
public class Mapp{
public static void main(String args[]){
//方式一:利用XmlBeanFactory先生成工厂,再加载路径CLASSPATH下的bean的配置文件。
XmlBeanFactory factory=new XmlBeanFactory(new ClassPathResource("Beans.xml"));
HelloWorld obj=(HelloWorld) factory.getBean("helloWorld");
//方式二:需要完整的绝对路径
ApplicationContext context = new FileSystemXmlApplicationContext("绝对路径/Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
//方式三:加载路径CLASSPATH下的bean的配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
//其他方式,不一一列出
}
}
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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--简单写法-->
<bean id="article" class="com.bean.Article">
<!--直接在XML配置中设置属性值-->
<property name="message" value="Hello World!"/>
</bean>
<beans>
作用域
作用域 | 描述 |
---|---|
singleton | 在spring IoC容器仅存在一个Bean实例,Bean以单例方式存在,默认值 |
prototype | 每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时,相当于执行newXxxBean() |
request | 每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境 |
session | 同一个HTTP Session共享一个Bean,不同Session使用不同的Bean,仅适用于WebApplicationContext环境 |
global-session | 一般用于Portlet应用环境,该运用域仅适用于WebApplicationContext环境 |
生命周期
- Bean的定义——Bean的初始化——Bean的使用——Bean的销毁
DI依赖注入
基于设置方式 / 构造函数注入
- Java 源码写法
package com.bean;
public class Article {
public Author editor;
public Author author;
//基于构造函数
public Article(Author author) {
this.author = author;
}
//设置值的方式
public void setEditor(Author editor) {
this.editor = editor;
}
public Author getEditor() {
return editor;
}
public Author getAuthor(){
return author;
}
}
- 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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--以上的写法比较完善的写法-->
<!--设置值写法-->
<bean id="article1" class="com.bean.Article">
<property name="editor" ref="art1" />
</bean>
<bean id="aut1" class="com.bean.Author" />
<!--构造函数写法-->
<bean id="article2" class="com.bean.Article">
<constructor-arg ref="art2"/>
</bean>
<bean id="aut2" class="com.bean.Author" />
<!--内部注入属性法-->
<bean id="article3" class="com.bean.Article">
<property name="editor">
<bean id="aut3" class="com.bean.Author" />
</property>
</bean>
<beans>
注入集合的写法
XML配置写法,Java代码参照:基于设置方式 / 构造函数注入写法
<?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-3.0.xsd">
<bean id="..." class="...">
<!-- Passing bean reference for java.util.List -->
<property name="addressList">
<list>
<ref bean="address1"/>
<ref bean="address2"/>
<value>Pakistan</value>
</list>
</property>
<!-- Passing bean reference for java.util.Set -->
<property name="addressSet">
<set>
<ref bean="address1"/>
<ref bean="address2"/>
<value>Pakistan</value>
</set>
</property>
<!-- Passing bean reference for java.util.Map -->
<property name="addressMap">
<map>
<entry key="one" value="INDIA"/>
<entry key ="two" value-ref="address1"/>
<entry key ="three" value-ref="address2"/>
</map>
</property>
</bean>
</beans>
自动装配
自动装配byName
XML配置写法,Java代码参照:基于设置方式 / 构造函数注入写法
<?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-3.0.xsd">
<!--通过属性值自动装配 -->
<bean id="article1" class="com.bean.Article" autowire="byName">
</bean>
<bean id="editor" class="com.bean.Author">
</bean>
<!--通过属性类型自动装配 -->
<bean id="article1" class="com.bean.Article" autowire="byType">
</bean>
<bean id="Author" class="com.bean.Author">
</bean>
<!--通过构造函数自动装配 -->
<bean id="article1" class="com.bean.Article" autowire="constructor">
</bean>
<bean id="Author" class="com.bean.Author">
</bean>
</beans>
注解配置
@Required 、@Autowired 、@Qualifier
- 注解配置-Java 源码配置三种方式:设置值,属性,构造函数
package com.bean;
public class Article {
//属性中使用注解@Autowired
//xml中配置多个相同类型的bean,想要用一个属性只为其中的一个进行装配则可以使用@Qualifier("editor2")
@Autowired
@Qualifier("editor2")
public Author editor;
public Author author;
public String message;
//基于构造函数
//构造函数使用注解@Autowired
@Autowired
public Article(Author author) {
this.author = author;
}
//设置值使用注解@Autowired
@Autowired
public void setEditor(Author editor) {
this.editor = editor;
}
public Author getEditor() {
return editor;
}
public Author getAuthor() {
return author;
}
//表明bean的属性值必须在xml中配置,否则容器抛出异常
@Required
public void setMessage(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
- 注解配置-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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--如果使用注解,则使用如下设置-->
<context:annotation-config/>
<bean id="article" class="com.bean.Article">
<property name="message" value="Zara" />
</bean>
<!--Java代码中使用了@Autowired,所有下面的bean会自动注入到Article中的editor【属性】/【构造函数】/【设置值】-->
<bean id="editor1" class="com.bean.Author">
</bean>
<bean id="editor2" class="com.bean.Author">
</bean>
</beans>
@Configuration 、@Bean @Import
- @configuration 表示这个类可以使用SpringIoc容器作为bean定义的来源。
- @Bean 表示Spring ,一个带有@Bean的注解返回一个对象
package com.bean;
import org.springframework.beans.factory.annotation.Autowired;
public class Student {
public String name;
public int number;
public Customer customer;
public void setName(String name) {
this.name=name;
}
public void setNumber(int number) {
this.number=number;
}
//使用注解自动注入
@Autowired
public void setCustomer(Customer customer) {
this.customer=customer;
}
public String getName() {
return name;
}
public int getNumber() {
return number;
}
public Customer getCustomer() {
return customer;
}
}
//基于Java的Bean配置,如果有多个配置,只需要使用@Import(OtherConfig.class)导入
package com.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.bean.Customer;
import com.bean.Student;
@Configuration
public class StudentConfig {
@Bean
public Student student() {
return new Student();
}
@Bean
public Customer customer() {
return new Customer();
}
//【依赖性】使用手动注入,也可以省略直接使用注解自动注入
@Bean
public Customer diStudent() {
Student student=student();
student.setCustomer(customer());
return student.getCustomer();
}
}
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.bean.Student;//忽略
import com.config.StudentConfig;
//测试Bean使用的方法
public class Test{
public static void main(String args[]) {
//第一种加载方法
//ApplicationContext context=new AnnotationConfigApplicationContext(StudentConfig.class);
/*******第二种加载方法start*******/
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext();
context.register(StudentConfig.class);
context.refresh();
/*******第二种加载方法end*******/
Student student=context.getBean(Student.class);
student.setName("小明");
student.setNumber(22);
System.out.println("姓名:"+student.getName()+" | 学号:"+student.getNumber());
}
}
事件处理
https://www.w3cschool.cn/wkspring/reap1icq.html