版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/Mythology_px/article/details/82903758
Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring的核心是控制反转(IoC)和面向切面(AOP)
IOC
package com.entity;
/*
* 学生实体类
*/
public class Student {
private String name;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
// show方法
public void show(){
System.out.println("姓名:"+name+" 年龄:"+age);
}
}
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- 配置service
<bean> 配置需要创建的对象
id :用于之后从spring容器获得实例时使用的
class :需要创建实例的全限定类名
-->
<!-- 描述Student类创建的方式 -->
<bean id="stu" class="com.entity.Student">
<property name="name" value="墨渐生微"></property>
<property name="age" value="21"></property>
</bean>
</beans>
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.entity.Student;
/*
* 测试
*/
public class Test {
public static void main(String[] args) {
// ① 获取spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
// ② 获取Student实例化对象,不需要new,直接从spring容器中获取
Student stu = (Student) ac.getBean("stu");
/*
* IOC:由容器控制程序之间的关系,
* 控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转
*/
stu.show();
}
}
接口IOC
package com.services;
/*
* 接口
*/
public interface IUsb {
public void work();
}
package com.services;
/*
* 接口实现类:充电器
*/
public class ChargerUsbImp implements IUsb{
private String name ;
public ChargerUsbImp() {
super();
// TODO Auto-generated constructor stub
}
public ChargerUsbImp(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void work() {
System.out.println(name+"正在充电中.........");
}
}
package com.services;
/*
* 接口实现类:硬盘
*/
public class DiskUsbImp implements IUsb{
private String name;
public DiskUsbImp(String name) {
super();
this.name = name;
}
public DiskUsbImp() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void work() {
System.out.println(name+"正在复制文件中.......");
}
}
<bean id="usb" class="com.services.DiskUsbImp">
<property name="name" value="金士顿硬盘"></property>
</bean>
<!--
<bean id="usb" class="com.services.ChargerUsbImp">
<property name="name" value="小米充电器"></property>
</bean>
-->
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.services.IUsb;
/*
* 测试
*/
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
// 通过接口多态,修改配置xml文件,可以实现接口多调用
IUsb iu =(IUsb) ac.getBean("usb");
iu.work();
}
}
依赖注入
即组件之间的依赖关系由容器在运行期决定,形象的来说,即由容器动态的将某种依赖关系注入到组件之中
依赖注入方式:手动装配 和 自动装配
手动装配:一般进行配置信息都采用手动
基于xml装配:构造方法、setter方法
基于注解装配:
构造方法
package com.entity;
public class Student {
private String name;
private int age;
public Student(String name, int age) {
System.out.println("构造方法运行中....");
this.name = name;
this.age = age;
}
}
<bean id="stu" class="com.entity.Student">
<!-- 构造方法注入:
name :参数的名称
value:设置普通数据
-->
<!-- 赋值的两种方式 -->
<constructor-arg name="name" value="Micro"></constructor-arg>
<constructor-arg name="age">
<value>21</value>
</constructor-arg>
</bean>
setter方法
package com.entity;
public class Student {
private String name;
private int age;
public void setName(String name) {
System.out.println("姓名设置中.......");
this.name = name;
}
public void setAge(int age) {
System.out.println("年龄设置中.......");
this.age = age;
}
}
<bean id="stu" class="com.entity.Student">
<!-- setter注入 -->
<property name="name" value="Micro"></property>
<property name="age">
<value>21</value>
</property>
</bean>
ref属性参考指定
package com.entity;
/*
* 班级实体类
*/
public class Classs {
private String clsno;
private String cname;
private String addr;
public void setClsno(String clsno) {
this.clsno = clsno;
}
public void setCname(String cname) {
this.cname = cname;
}
public void setAddr(String addr) {
this.addr = addr;
}
}
package com.entity;
public class Student {
private String name;
private int age;
private Classs classs;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setClasss(Classs classs) {
this.classs = classs;
}
}
<!-- 注入Classs -->
<bean id="classs" class="com.entity.Classs">
<property name="clsno" value="8801"></property>
<property name="cname" value="Java"></property>
<property name="addr" value="Beijing"></property>
</bean>
<!-- 注入Student -->
<bean id="stu" class="com.entity.Student">
<property name="name" value="Micro"></property>
<property name="age" value="21"></property>
<!-- 属性为引用数据类型:ref指向容器中的其他bean -->
<!--
<property name="classs" ref="classs"></property>
-->
<property name="classs">
<ref bean="classs"/>
</property>
</bean>
集合注入
package com.entity;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/*
* 集合测试类
*/
public class Tests {
private List<String> list;
private Set<String> set;
private Map<String,String> map;
private Properties prop;
public void setList(List<String> list) {
this.list = list;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setProp(Properties prop) {
this.prop = prop;
}
}
<bean id="test" class="com.entity.Tests">
<!-- List集合 -->
<property name="list">
<list>
<value>墨渐生微</value>
<value>Micro</value>
</list>
</property>
<!-- Set集合 -->
<property name="set">
<set>
<value>Peter</value>
<value>Myth</value>
</set>
</property>
<!-- Map集合 -->
<property name="map">
<map>
<entry key="A" value="Rose"></entry>
<entry key="B" value="Gole"></entry>
</map>
</property>
<!-- Properties -->
<property name="prop">
<props>
<prop key="A">Sealr</prop>
<prop key="B">Bob</prop>
</props>
</property>
</bean>
自动装配: autowire
no:默认情况下,不自动装配,通过“ref”attribute手动设定
buName:根据Property的Name自动装配,如果一个bean的name,和另一个bean中的Property的name相同,则自动装配这个bean到Property中
<!-- 注入班级Classs -->
<bean id="classs" class="com.entity.Classs">
<property name="cname" value="Java"></property>
<property name="addr" value="NewYork"></property>
</bean>
<!-- byName:通过Student类的班级属性名查找id为此的bean自动装配 -->
<bean id="stu" class="com.entity.Student" autowire="byName">
<property name="name" value="Micro"></property>
<property name="age" value="21"></property>
</bean>
byType:根据Property的数据类型(Type)自动装配,如果一个bean的数据类型,兼容另一个bean中Property的数据类型,则自动装配
<!-- 注入Classs -->
<bean id="classs" class="com.entity.Classs">
<property name="cname" value="Java"></property>
<property name="addr" value="NewYork"></property>
</bean>
<!-- byType:通过Student类的班级属性的类型查找此类型的bean自动装配 -->
<bean id="stu" class="com.entity.Student" autowire="byType">
<property name="name" value="Micro"></property>
<property name="age" value="21"></property>
</bean>
constructor :根据构造函数参数的数据类型,进行byType模式的自动装配
autodetect:如果发现默认的构造函数,用constructor模式,否则,用byType模式