Spring Bean管理(XML方式)
1.通过maven建立spring依赖:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.yzy</groupId> <artifactId>springIoc</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>springIoc Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.2.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>5.2.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> <build> <finalName>springIoc</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>
2.Ioc(控制反转)将新建对象的权限赋予spring
新建applicationContext.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="Demo1" class="org.yzy.Demo1"></bean> <!-- 将Demo1这个类控制反转--> </beans>
通过Application来新建一个对象:
public void test(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); Demo1 demo1 =(Demo1) applicationContext.getBean("Demo1"); demo1.sayHello(); }
Spring的工厂类:
三种实例化Bean的方式
使用类构造器实例化(默认无参数):
<!--第一种:无参构造器的方式--> <bean id="bean1" class="com.imooc.ioc.demo2.Bean1"/>
使用静态工厂方法实例化(简单工厂模式)
<!--第二种:静态工厂的方式--> <bean id="bean2" class="com.imooc.ioc.demo2.Bean2Factory" factory-method="createBean2"/>
使用实例工厂方法实例化(工厂方法模式)
<!--第三种:实例工厂的方式--> <bean id="bean3Factory" class="com.imooc.ioc.demo2.Bean3Factory"/> <bean id="bean3" factory-bean="bean3Factory" factory-method="createBean3"/>
关于bean标签下的id、name、class和scope
bean的生命周期:
spring的属性注入
例子:
package org.yzy; import java.util.*; public class collectionDemo { private String name; private int age; private String [] strings; private List<String> lists; private Set<String> sets; private Properties properties; private Map<String,String> maps; private Demo1 demo1; @Override public String toString() { return "collectionDemo{" + "name='" + name + '\'' + ", age=" + age + ", strings=" + Arrays.toString(strings) + ", lists=" + lists + ", sets=" + sets + ", properties=" + properties + ", maps=" + maps + ", demo1=" + demo1 + '}'; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String[] getStrings() { return strings; } public void setStrings(String[] strings) { this.strings = strings; } public List<String> getLists() { return lists; } public void setLists(List<String> lists) { this.lists = lists; } public Set<String> getSets() { return sets; } public void setSets(Set<String> sets) { this.sets = sets; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public Map<String, String> getMaps() { return maps; } public void setMaps(Map<String, String> maps) { this.maps = maps; } public Demo1 getDemo1() { return demo1; } public void setDemo1(Demo1 demo1) { this.demo1 = demo1; } }
<?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="Demo1" class="org.yzy.Demo1"></bean> <bean id="collection" class="org.yzy.collectionDemo"> <property name="name" value="#{'yzy'}"></property> <property name="age" value="#{18}"></property> <property name="demo1" value="#{Demo1}"></property> <property name="strings"> <list> <value>hhh</value> <value>ttt</value> <value>aaa</value> </list> </property> <property name="lists"> <list> <value>123</value> <value>456</value> <value>789</value> </list> </property> <property name="maps"> <map> <entry key="yyy" value="nb"></entry> <entry key="xxx" value="bnb"></entry> <entry key="hhh" value="kx"></entry> </map> </property> <property name="properties"> <props> <prop key="yyy">nb</prop> <prop key="xxx">bnb</prop> <prop key="hhh">kx</prop> </props> </property> <property name="sets"> <set> <value>hhh</value> <value>yyy</value> <value>xxx</value> </set> </property> </bean> </beans>