目录标题
- HelloSpring
- 1、导入Jar包
- 2、编写代码
- 3 Spring的IoC
- 4、IOC创建对象方式
- 1.通过无参构造方法来创建
- 2.通过有参构造方法来创建
- beans.xml 有三种方式编写
- 5、Spring配置
HelloSpring
1、导入Jar包
注 : spring 需要导入commons-logging进行日志记录 . 我们利用maven , 他会自动下载对应的依赖项 .
<dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.1.10.RELEASE</version></dependency>
2、编写代码
编写一个Hello实体类
package nuc.ss.pojo;public class Hello {private String str;public String getStr() {return str;}public void setStr(String str) {this.str = str;}@Overridepublic String toString() {return "Hello{" +"str='" + str + '\'' +'}';}
}
编写我们的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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--bean就是java对象 , 由Spring创建和管理类型 变量名 = new 类型();Hello hello = new Hello();bean = 对象 new Hello();id = 变量名class = new的对象property 相当于给对象中的属性设置一个变量--><bean id="hello" class="nuc.ss.pojo.Hello"><property name="str" value="Spring"/></bean></beans>
我们可以去进行测试了 .
import nuc.ss.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);}
}
执行结果:
Hello{str='Spring'}Process finished with exit code 0
3.3、思考
类型 变量名 = new 类型();
Hello hello = new Hello();
bean = 对象 new Hello();<bean id="hello" class="nuc.ss.pojo.Hello"><property name="str" value="Spring"/>
</bean>id = 变量名
class = new的对象
property 相当于给对象中的属性设置一个变量 比如属性 private UserDao userdao;
Hello 对象是谁创建的 ? 【 hello 对象是由Spring创建的 】
Hello 对象的属性是怎么设置的 ? 【hello 对象的属性是由Spring容器设置的 】
这个过程就叫控制反转 :
3 Spring的IoC
控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 也就是要new一个对象; 使用Spring后 , 对象是由Spring来创建的
反转 : 程序本身不创建对象 , 而变成被动的接收对象 .
依赖注入 : 就是利用set方法来进行注入的.
Hello.java 中必须有set,才能 通过beans.XML进行值注入;没有的话,beans.XML会报错。
public void setStr(String str) {
this.str = str;
}
有树叶,说明此类被spring 托管了,如果没有的话,说明还没注册到xml里,还不能用。
IOC是一种编程思想,由主动的编程变成被动的接收
可以通过newClassPathXmlApplicationContext去浏览一下底层源码 .
// 获取Spring的上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext(“beans.xml”);
java确实是单继承,一个类只能继承一个类,但是实现接口可以多个,实现接口不等于继承
1.4、修改案例一
我们在案例一中, 新增一个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 http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 现在这套程序是:你告诉楼下餐厅,你要哪些菜,客人来的时候,餐厅把做好的你需要的菜送上来-->
<!-- mysqlImpl 就相当于 new了一个 UserDaoMysqlImpl --><bean id="mysqlImpl" class="nuc.ss.dao.UserDaoMysqlImpl"/><bean id="oracleImpl" class="nuc.ss.dao.UserDaoOracleImpl"/><bean id="UserServiceImpl" class="nuc.ss.service.UserServiceImpl"><!--ref: 引用Spring容器中创建好的对象value: 具体的值,基本数据类型--><property name="userDao" ref="mysqlImpl"/></bean></beans>
使用测试!
import nuc.ss.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {public static void main(String[] args) {// 获取ApplicationContext:拿到Spring的容器 new cpx 通过类路径下的xml文件 来获取// 传一个配置文件进去就行了 "beans.xml"// ApplicationContext context 向上转型,通过配置文件xml,拿到spring的容器,里面有注册好的对象// 相当于把对象全放婚介所了,先去把婚介所搬来,然后在里面选要用的对象ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");//容器在手,天下我有,需要什么,就直接get什么//用户有需求,之前写死了,程序员要改程序,后面要用户改接口;现在谁都不用改,只更改配置文件可以不改变程序原有的代码userServiceImpl.getUser();}
}
执行结果
Mysql获取用户数据Process finished with exit code 0
更改配置文件可以不改变程序原有的代码 即可变成 UserDaoOracleImpl
<?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"><!-- 现在这套程序是:你告诉楼下餐厅,你要哪些菜,客人来的时候,餐厅把做好的你需要的菜送上来-->
<!-- mysqlImpl 就相当于 new了一个 UserDaoMysqlImpl --><bean id="mysqlImpl" class="nuc.ss.dao.UserDaoMysqlImpl"/><bean id="oracleImpl" class="nuc.ss.dao.UserDaoOracleImpl"/><bean id="UserServiceImpl" class="nuc.ss.service.UserServiceImpl"><!--ref: 引用Spring容器中创建好的对象value: 具体的值,基本数据类型-->
<!-- <property name="userDao" ref="mysqlImpl"/>-->
<!-- 更改配置文件可以不改变程序原有的代码 即可变成 UserDaoOracleImpl --><property name="userDao" ref="oracleImpl"/></bean></beans>
使用测试! 直接跑上面的MyTest
Oracle获取用户数据Process finished with exit code 0
OK , 到了现在 , 我们彻底不用再程序中去改动了 ,改程序要重新发包的 要重新编译的
。 要实现不同的操作 , 只需要在xml配置文件中进行修改 , 所谓的IoC,
IoC一句话搞定 : 对象由Spring 来创建 , 管理 , 装配 !
4、IOC创建对象方式
1.通过无参构造方法来创建
User.java
public class User {private String name;public User() {System.out.println("user无参构造方法");}public void setName(String name) {this.name = name;}public void show(){System.out.println("name="+ name );}
}
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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="user" class="com.kuang.pojo.User"><property name="name" value="kuangshen"/></bean>
</beans>
测试类
@Test
public void test(){ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");//在执行getBean的时候, user已经创建好了 , 通过无参构造User user = (User) context.getBean("user");//调用对象的方法 .user.show();
}
结果可以发现,在调用show方法之前,User对象已经通过无参构造初始化了!
2.通过有参构造方法来创建
UserT . java
public class UserT {private String name;public UserT(String name) {this.name = name;}public void setName(String name) {this.name = name;}public void show(){System.out.println("name="+ name );}
}
beans.xml 有三种方式编写
<!-- 第一种根据index参数下标设置 -->
<bean id="userT" class="com.kuang.pojo.UserT"><!-- index指构造方法 , 下标从0开始 --><constructor-arg index="0" value="kuangshen2"/>
</bean>
<!-- 第二种根据参数名字设置 -->
<bean id="userT" class="com.kuang.pojo.UserT"><!-- name指参数名 --><constructor-arg name="name" value="kuangshen2"/>
</bean>
<!-- 第三种根据参数类型设置 -->
<bean id="userT" class="com.kuang.pojo.UserT"><constructor-arg type="java.lang.String" value="kuangshen2"/>
</bean>
测试
@Test
public void testT(){
ApplicationContext context = new ClassPathXmlApplicationContext(“beans.xml”);
UserT user = (UserT) context.getBean(“userT”);
user.show();
}
结论:在配置文件加载的时候。其中管理的对象都已经初始化了!
当你new ClassPathXmlApplicationContext()的时候,bean里面的对象都被创建了, 在你getBean的时候,里面的对象都是使用的一个
5、Spring配置
5.1. 别名
alias 设置别名 , 为bean设置别名 , 可以设置多个别名