Spring02-快速上手Spring
HelloSpring
- 编写实体类
public class Hello {
private String str;
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
@Override
public String toString() {
return "Hello{" +
"str='" + str + '\'' +
'}';
}
}
- 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">
<!--使用Spring来创建对象,在Spring中这些都成为Bean-->
<!--bean就是java对象 , 由Spring创建和管理
类型 变量名 = new 类型();
Hello hello = new Hello();
id = 变量名
class = new 的对象
property 相当于给对象中的属性设置一个值
-->
<bean id="hello" class="com.byx.pojo.Hello">
<property name="str" value="String"/>
</bean>
</beans>
- Test
public void testHello(){
//获取Spring的上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//我们的对象现在都在spring中管理,要使用,直接在里面取
Hello hello = (Hello) context.getBean("hello");
System.out.println(hello.toString() );
}
- 思考:
- Hello 对象是谁创建的?hello 对象是由Spring创建的
- Hello 对象的属性是怎么设置的?hello 对象的属性是由Spring容器设置的。这个过程就叫控制反转
- 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的
- 反转 : 程序本身不创建对象 , 而变成被动的接收对象 .
- 依赖注入 : 就是利用set方法来进行注入的。若取消相应实体类的set方法,xml文件中property标签会报错
IOC是一种编程思想,由主动的编程变成被动的接收
修改案例一
- 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="MysqlImpl" class="com.kuang.dao.impl.UserDaoMySqlImpl"/>
<bean id="OracleImpl" class="com.kuang.dao.impl.UserDaoOracleImpl"/>
<bean id="ServiceImpl" class="com.kuang.service.impl.UserServiceImpl">
<!--注意: 这里的name并不是属性 , 而是set方法后面的那部分, 首字母小写
在配置反射过程中,其实都是取的set方法的后缀名,并不是属性。
若属性名更改,对反射无影响。但是setXX更改,则有影响。
-->
<!--引用另外一个bean , 不是用value 而是用 ref-->
<property name="userDao" ref="OracleImpl"/>
</bean>
</beans>
IOC创建对象的方式
使用无参构造创建对象。(默认实现)
- 之前的方式都是使用无参构造
使用有参构造创建对象。
- 通过下标赋值。有几个参数,就通过index索引依次赋值
<!--第一种,通过下标赋值-->
<bean id="user" class="com.byx.pojo.User">
<constructor-arg index="0" value="baibaibai"/>
</bean>
- 通过参数的类型匹配赋值。遇到多个参数类型一致,就需要创建多个标签,会根据标签的顺序赋值。不建议使用
<!--第二种,通过类型匹配赋值-->
<bean id="user" class="com.byx.pojo.User">
<constructor-arg type="java.lang.String" value="baibaibai"/>
</bean>
- 通过参数名赋值
<!--第三种,直接通过参数名赋值-->
<bean id="user" class="com.byx.pojo.User">
<constructor-arg name="name" value="baibai"/>
</bean>
注:实际上在ApplicationContext context = new ClassPathXmlApplicationContext(“beans.xml”); 加载完配置文件后,就已经将xml定义的bean听过所设置的无参或者有参构造函数,构造出相应的对象。也就是说,在配置文件加载的时候,容器中管理的对象就已经初始化了。
Spring配置
alias(别名)
<!--设置别名:在获取Bean的时候可以使用别名获取-->
<alias name="userT" alias="userNew"/>
bean
<!--bean就是java对象,由Spring创建和管理-->
<!--
id 是bean的标识符,要唯一,如果没有配置id,name就是默认标识符
如果配置id,又配置了name,那么name是别名
name可以设置多个别名,可以用逗号,分号,空格隔开
如果不配置id和name,可以根据applicationContext.getBean(.class)获取对象;
class是bean的全限定名=包名+类名
-->
<bean id="hello" name="hello2 h2,h3;h4" class="com.kuang.pojo.Hello">
<property name="name" value="Spring"/>
</bean>
import
- 一般用于团队开发实现,可以将多个配置文件,合并为一个。使用时使用总的配置文件即可。
<import resource="{path}/beans.xml"/>