1 Spring基本流程
1.1 配置元数据:
https://docs.spring.io/spring/docs/5.2.8.RELEASE/spring-framework-reference/core.html#beans-factory-metadata(从这找)
<?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">
</beans>
1.2 实例化容器:
//填写参数即可,这里是XML文件
ApplicationContext context = new ClassPathXmlApplicationContext("***");
注意:
2 Hello对象由Spring创建,其创建过程如下:
传统:类型 变量名 = new 类型();
bean = 对象 new Hello();
id = 变量名
class = new de 对象
property相当于给对象中的属性设置一个值。
相当于开发人员做一下配置,其余交给容器去做。
<!--使用Spring来创建对象,在Spring中这些都称为Bean-->
<bean id = "hello" class = "com.kuang.pojo.Hello">
<property name = "str" value="Spring"/>
</bean>
对象由Spring创建,管理,装配。
3 实例
3.1 配置XML(基于上文Spring01)
<?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>
3.2 客户端调用
import com.kuang.Dao.UserDaoMysqlImpl;
import com.kuang.Dao.UserDaoOracleImpl;
import com.kuang.service.UserService;
import com.kuang.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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();
}
}
在这里,用户只需配置bean.xml文件即可,不需要怎么修改代码。
3.3 beans.xml 有三种方式编写
官方文档来源:https://docs.spring.io/spring/docs/5.2.8.RELEASE/spring-framework-reference/core.html#beans-constructor-injection
<!-- 第一种根据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>
3.4 强化Spring配置
alias 设置别名 , 为bean设置别名 , 可以设置多个别名
<!--设置别名:在获取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来实现 .
<import resource="{path}/beans.xml"/>