目录
1.HelloSpring
1.1使用过程
1.导入jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
2.编写一个java类
public class Hello {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("Hello,"+ name );
}
}
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
http://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.adie.pojo.Hello">
<property name="str" value="Spring"/>
</bean>
</beans>
4.进行测试
import com.adie.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 对象是谁创建的 ? hello 对象是由Spring创建的
- Hello 对象的属性是怎么设置的 ? hello 对象的属性是由Spring容器设置的
这个过程就叫控制反转(IOC) :
- 控制 : 谁来控制对象的创建 , 传统应用程序的对象是由程序本身控制创建的 , 使用Spring后 , 对象是由Spring来创建的
- 反转 : 程序本身不创建对象 , 而变成被动的接收对象 .
依赖注入 : 就是利用set方法来进行注入的.(在上述案例中,如果我们在Hello的java类中删除set方法,在beans.xml中将会报错)
IOC是一种编程思想,由主动的编程变成被动的接收
可以通过ClassPathXmlApplicationContext类去浏览一下底层源码 。
1.2修改案例一
在上一个博客中的案例,新增一个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">
<bean id="mysqlImpl" class="com.adie.dao.UserDaoMysqlImpl"/>
<bean id="oracleImpl" class="com.adie.dao.UserDaoOracleImpl"/>
<bean id="UserServiceImpl" class="com.adie.service.UserServiceImpl">
<!--
ref:引用Spring容器中创建好的对象
value:具体的值,基本数据类型
-->
<property name="userDao" ref="mysqlImpl"/>
</bean>
</beans>
测试
@Test
public void test2(){
//获取ApplicationContext;拿到Spring的容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//容器在手,需要什么就直接get什么
UserServiceImpl userServiceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");
userServiceImpl.getUser();
}
到了现在 , 我们彻底不用再程序中去改动了 , 要实现不同的操作 , 只需要在xml配置文件中进行修改 , 所谓的IoC,就是 对象由Spring 来创建 , 管理 , 装配 !
2.IOC创建对象的方式
2.1使用无参构造创建对象,默认
1、User.java
package com.adie.pojo;
public class User {
private String name;
public User(String name){
this.name=name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void show(){
System.out.println("name="+name);
}
}
2、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="user" class="com.kuang.pojo.User">
<property name="name" value="adie"/>
</bean>
</beans>
3、测试类
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//在执行getBean的时候, user已经创建好了 , 通过无参构造
User user = (User) context.getBean("user");
//调用对象的方法 .
user.show();
}
结果发现,在调用show方法之前,User对象已经通过无参构造初始化了!

2.2通过有参构造创建对象
1.下标赋值
<bean id="user" class="com.adie.pojo.User">
<constructor-arg index="0" value="a碟碟"/>
</bean>
2.通过类型创建,不建议使用
Spring框架入门与IOC深入理解

本文详细介绍了Spring框架的基本使用,包括导入jar包、创建Java类、编写Spring配置文件并进行测试。讲解了IOC(控制反转)的概念,即对象由Spring容器创建和管理,并通过setter方法进行依赖注入。还探讨了无参和有参构造器创建对象的方式,以及别名、Bean配置和import的用法。此外,文章通过实例详细展示了如何通过setter注入复杂类型的属性,并介绍了p命名空间和c命名空间的注入方式。最后,讨论了Bean的作用域,包括Singleton和Prototype模式。
最低0.47元/天 解锁文章
918

被折叠的 条评论
为什么被折叠?



