spring 核心内容是 IOC和 AOP
IOC(控制反转:Inverse of Control ),又称作 依赖注入,是一种重要的面向对象编程的法则来削减计算机程序
的耦合问题
1、创建 spring 项目
2、在项目中创建两个实体类
package com.java.service;
public class ZhangSan implements Tester{
public void test(){
System.out.println("张三-测试程序");
}
}
package com.java.service;
public class Lisi implements Tester{
public void test(){
System.out.println("李四-测试程序");
}
}
3、创建一个管理类
package com.java.service;
public class JavaWork {
private Tester tester;
public void setTester(Tester tester) {
this.tester = tester;
}
public void doTest(){
/*ZhangSan zhangsan=new ZhangSan();
zhangsan.test();*/
tester.test();
}
}
4、将两个实体注入 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 id="zhangsan" class="com.java.service.ZhangSan"></bean>
<bean id="lisi" class="com.java.service.Lisi"></bean>
<bean id="javaWork" class="com.java.service.JavaWork">
<property name="tester" ref="lisi"></property>
</bean>
</beans>
5、测试
package com.java.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java.service.JavaWork;
public class Test2 {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
JavaWork javaWork=(JavaWork)ac.getBean("javaWork");
javaWork.doTest();
}
}
这就是简单依赖注入~~
依赖注入的方式:
1,属性注入;
2,构造函数注入;(通过类型;通过索引;联合使用)
3,工厂方法注入;(非静态工厂,静态工厂)
4,泛型依赖注入;
<?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="people0" class="com.java.entity.People"></bean>
<bean id="people1" class="com.java.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
</bean>
<bean id="people2" class="com.java.entity.People">
<constructor-arg type="int" value="2"></constructor-arg>
<constructor-arg type="String" value="李四"></constructor-arg>
<constructor-arg type="int" value="22"></constructor-arg>
</bean>
<bean id="people3" class="com.java1234.entity.People">
<constructor-arg index="0" value="3"></constructor-arg>
<constructor-arg index="1" value="王五"></constructor-arg>
<constructor-arg index="2" value="55"></constructor-arg>
</bean>
<bean id="people4" class="com.java.entity.People">
<constructor-arg index="0" type="int" value="4"></constructor-arg>
<constructor-arg index="1" type="String" value="招六"></constructor-arg>
<constructor-arg index="2" type="int" value="66"></constructor-arg>
</bean>
<bean id="peopleFactory" class="com.java.factory.PeopleFactory"></bean>
<bean id="people5" factory-bean="peopleFactory" factory-method="createPeople"></bean>
<bean id="people6" class="com.java.factory.PeopleFactory2" factory-method="createPeople"></bean>
</beans>
注入参数:
1,基本类型值;
2,注入 bean;
3,内部 bean;
4,null 值;
5,级联属性;
6,集合类型属性
<?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="people1" class="com.java.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
</bean>
<bean id="dog1" class="com.java.entity.Dog">
<property name="name" value="Jack"></property>
</bean>
<bean id="people2" class="com.java.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<property name="dog" ref="dog1"></property>
</bean>
<bean id="people3" class="com.java.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<property name="dog">
<bean class="com.java.entity.Dog">
<property name="name" value="Tom"></property>
</bean>
</property>
</bean>
<bean id="people4" class="com.java.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<property name="dog">
<null></null>
</property>
</bean>
<!-- <bean id="people5" class="com.java.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<property name="dog.name" value="Jack2"></property>
</bean> -->
<bean id="people6" class="com.java.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<property name="dog" ref="dog1"></property>
<property name="hobbies">
<list>
<value>唱歌</value>
<value>跳舞</value>
</list>
</property>
<property name="loves">
<set>
<value>唱歌2</value>
<value>跳舞2</value>
</set>
</property>
<property name="works">
<map>
<entry>
<key><value>上午</value></key>
<value>写代码</value>
</entry>
<entry>
<key><value>下午</value></key>
<value>测试代码</value>
</entry>
</map>
</property>
<property name="addresses">
<props>
<prop key="address1">aaaaa</prop>
<prop key="address2">bbbbb</prop>
</props>
</property>
</bean>
</beans>
spring 自动装配
通过配置 default-autowire 属性,Spring IOC 容器可以自动为程序注入 bean;默认是 no,不启用自动装配;
default-autowire 的类型有 byName,byType,constructor;
byName:通过名称进行自动匹配;
byType:根据类型进行自动匹配;
constructor:和 byType 类似,只不过它是根据构造方法注入而言的,根据类型,自动注入;
建议:自动装配机制慎用,它屏蔽了装配细节,容易产生潜在的错误;
<?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"
default-autowire="constructor">
<bean id="dog2" class="com.java.entity.Dog">
<property name="name" value="Jack"></property>
</bean>
<bean id="people1" class="com.java.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
</bean>
</beans>
<!------ 这是分隔线 ------>
<?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="dog" class="com.java.entity.Dog" scope="prototype">
<property name="name" value="Jack"></property>
</bean>
<bean id="people1" class="com.java.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<lookup-method name="getDog" bean="dog"/>
</bean>
</beans>
<!------ 这是分隔线 ------>
<?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="people1" class="com.java.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<replaced-method name="getDog" replacer="people2"></replaced-method>
</bean>
<bean id="people2" class="com.java.entity.People2"></bean>
</beans>
bean 之间的关系
1,继承;
2,依赖;
3,引用
<?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="dog" class="com.java.entity.Dog">
<property name="name" value="jack"></property>
</bean>
<bean id="abstractPeople" class="com.java.entity.People" abstract="true">
<property name="className" value="高三5班"></property>
<property name="age" value="19"></property>
</bean>
<bean id="zhangsan" parent="abstractPeople" depends-on="autority">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="lisi" parent="abstractPeople">
<property name="id" value="2"></property>
<property name="name" value="李四"></property>
<property name="age" value="20"></property>
<property name="dog" ref="dog"></property>
</bean>
<bean id="autority" class="com.java.service.Authority"></bean>
</beans>
bean 的作用范围
1,singleton Spring ioc 容器中仅有一个 Bean 实例,Bean 以单例的方式存在;
2,prototype 每次从容器中调用 Bean 时,都返回一个新的实例;
3,request 每次 HTTP 请求都会创建一个新的 Bean;
4,session 同一个 HTTP Session 共享一个 Bean;
5,global session 同一个全局 Session 共享一个 Bean,一般用于 Portlet 应用环境;
6,application 同一个 Application 共享一个 Bean;
<?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="dog" class="com.java.entity.Dog" scope="singleton">
<property name="name" value="jack"></property>
</bean>
</beans>
到此结束啦~~~
本文深入讲解Spring框架的核心概念IOC(控制反转),通过示例演示如何实现依赖注入,并介绍依赖注入的不同方式,包括属性注入、构造函数注入等。此外,还探讨了Spring自动装配的机制及其注意事项。
1749

被折叠的 条评论
为什么被折叠?



