HelloSpring
1、创建spring-02-hellospring
2、添加Hello实体类
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 + '\'' +
'}';
}
}
3、编写spring配置文件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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 使用sring来创建对象,在spring这些都称为Bean-->
<!-- 类型变量名=new类型();-->
<!-- HelLo hello = new HelLo();-->
<!-- id=变量名cLass =new的对象;-->
<!-- property相当于给对象中的属性设置一个值! -->
<bean id="hello" class="com.gx.pojo.Hello">
<property name="str" value="Spring" />
</bean>
</beans>
4、测试
public class MyTest {
public static void main(String[] args) {
//获取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方法来进行注入的
IOC是一种编程思想,由主动的编程变成被动的接收
可以通过newClassPathXmlApplicationContext去浏览一下底层源码
在模块spring-01-ioc1,添加一个配置文件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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mysqlImpl" class="com.gx.dao.UserDaoMysqlImpl" />
<bean id="oracleImpl" class="com.gx.dao.UserDaoOracleImpl" />
<bean id="UserServiceImpl" class="com.gx.service.UserServiceImpl">
<!-- ref:引用Spring容器中创建好的对象
value:具体的值,基本数据类型!
-->
<property name="userDao" ref="mysqlImpl"/>
</bean>
</beans>
测试
public class MyTest {
public static void main(String[] args) {
//获取ApplicationContext,拿到Spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//容器在手,天下我有,需要什么,就直接get什么
UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
userServiceImpl.getUser();
}
}
OK,到了现在,我们彻底不用再程序中去改动了,要实现不同的操作,只需要在xml配置文件中进行修改,所谓的IOC,一句话搞定:对象由Spring来创建,管理,装配!
IOC创建对象的方式
新建子模块spring-03-ioc2
添加User实体类
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User() {
System.out.println("User的无参构造!");
}
public void show(){
System.out.println("name"+name);
}
}
新建配置文件beans.xml
1、使用无参构造创建对象,默认!
2、假设我们要使用有参构造创建对象
- 下标赋值
<!-- 第一种,下标赋值-->
<bean id="user" class="com.gx.dao.User">
<constructor-arg index="0" value="shen" />
</bean>
- 类型
<!--第二种,通过类型创建,不建议使用-->
<bean id="user" class="com.gx.dao.User">
<constructor-arg type="java.lang.String" value="shen" />
</bean>
- 参数名
<!--第三种,直接通过参数名来设置-->
<bean id="user" class="com.gx.dao.User">
<constructor-arg name="name" value="shen" />
</bean>
总结:在配置文件加载的时候,容器中管理的对象就已经初始化了!
Spring配置
1、别名
<!-- 别名,如果添加了别名,我们也可以使用别名获取到这个对象-->
<alias name="user" alias="sdfglj" />
2、bean的配置
<!-- id : bean的唯一标识符, 也就是相当于我们学的对象名
class: bean 对象所对应的全限定名: 包名+类名
name :也是别名,而且name可以同时取多个别名
-->
<bean id="userT" class="com.gx.dao.UserT" name="user2 u2,u3;u4">
<property name="name" value="sezi" />
</bean>
3、import
这个import,一般用于团队开发使用,他可以将多个配置文件,导入合并为一个
假设,现在项目中有多个人开发,这三个人复制不同的类开发,不同的类需要注册在不同的bean中,我们可以利用import将所有人的beans.xml合并为一个总的!
-
张三
-
李四
-
王五
applicationContext.xml
<import resource="beans.xml" />
<import resource="bean2.xml" />
<import resource="bean3.xml" />
使用的时候,直接使用总的配置就可以了