HelloSpring
大约 3 分钟Spring全家桶Spring入门
创建子项目spring-02-hellospring
Hello.java
package com.kuang.pojo;
/**
* @author Administrator
*/
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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--使用Spring来创建对象,在Spring中这些都成为bean-->
<bean id="hello" class="com.kuang.pojo.Hello" >
<property name="str" value="Spring"/>
</bean>
</beans>
MyTest.java
import com.kuang.pojo.Hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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{str='Spring'}
思考问题
Hello对象是谁创建的?
- hello对象是由Spring创建的。
Hello对象的属性是怎么设置的?
- hello对象的属性是由Spring容器设置的
<?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">
<!--使用Spring来创建对象,在Spring中这些都成为bean
类型 变量名 = new 类型()
Hello hello = new Hello();
bean = 对象 new Hello()
id = 变量名
class = new 的对象
property 相当于给对象中的属性设置一个值!
-->
<bean id="hello" class="com.kuang.pojo.Hello" >
<property name="str" value="Spring"/>
</bean>
</beans>
提示
这个过程就叫控制反转。
控制:谁来控制对象的创建,传统应用程序的对象是由程序本身控制创建的。使用Spring后,对象是由Spring来创建的。
反转:程序本身不创建对象,而变成被动的接受对象。
依赖注入:就是利用set方法来进行注入的。
package com.kuang.pojo;
/**
* @author Administrator
*/
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 + '\'' +
'}';
}
}
必须有set方法。
IOC是一种编程思想,由主动的编程变成被动的接收。
可以通过new ClassPathXmlApplicationContext去浏览一下底层源码。
设置多个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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mysqlImpl" class="com.kuang.dao.UserDaoMysqlImpl"/>
<bean id="oracleImpl" class="com.kuang.dao.UserDaoOracleImpl"/>
<bean id="userServiceImpl" class="com.kuang.service.UserServiceImpl">
<!--ref:引用Spring容器中创建好的对象
value:具体的值,基本数据类型
-->
<property name="userDao" ref="oracleImpl"/>
</bean>
</beans>
MyTestSpring.java
import com.kuang.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTestSpring {
public static void main(String[] args) {
//获取ApplicationContext
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("userServiceImpl");
userServiceImpl.getUser();
}
}
结果:
Oracle获取数据
我现在需要使用MySQL获取数据,直接修改配置文件,其他无需任何改动。
<?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.kuang.dao.UserDaoMysqlImpl"/>
<bean id="oracleImpl" class="com.kuang.dao.UserDaoOracleImpl"/>
<bean id="userServiceImpl" class="com.kuang.service.UserServiceImpl">
<!--ref:引用Spring容器中创建好的对象
value:具体的值,基本数据类型
-->
<property name="userDao" ref="mysqlImpl"/>
</bean>
</beans>
结果:
Mysql获取用户数据
OK,到了现在,我们彻底不用再去程序中改动。要实现不同的操作,只需要在xml配置文件中进行修改。所谓的IoC,一句话搞定:对象由Spring创建、管理、装配!