spring.xmljava
<?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="userService" class="com.xxxx.service.UserService"></bean> </beans>
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
ApplicationContext ac = new FileSystemXmlApplicationContext("C:/IdeaWorkspace/spring01/src/main/resources/spring.xml");
Spring 框架启动时能够加载多个配置文件到环境中。对于比较复杂的项目,可能对应的配置文件有多个,项目在启动部署时会将多个配置文件同时加载进来。spring
service.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="userService" class="com.xxxx.service.UserService"></bean> </beans>
dao.xmlide
<?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="userDao" class="com.xxxx.dao.UserDao"></bean> </beans>
// 同时加载多个资源文件 ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml","dao.xml");
spring.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"> <!--导入须要包含的资源文件--> <import resource="service.xml"/> <import resource="dao.xml"/> </beans>
加载时只需加载总的配置文件便可spa
// 加载总的资源文件 ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
若有疑问,可加入群:10803-55292,输入暗号13,便可有大佬帮助code
Spring IOC 容器 Bean 对象实例化
注:经过默认构造器建立 空构造方法必须存在 不然建立失败xml
设置配置文件 spring.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="userService" class="com.xxxx.service.UserService"></bean> </beans>
获取实例化对象资源
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); UserService userService = (UserService) ac.getBean("userService"); userService.test();
注:
定义静态工厂类
package com.xxxx.factory; import com.xxxx.service.UserService; /** * 定义静态工厂类 */ public class StaticFactory { /** * 定义对应的静态方法,返回实例化对象 * @return */ public static UserService createUserService() { return new UserService(); } }
设置配置文件 spring.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="userService" class="com.xxxx.factory.StaticFactory" factory-method="createUserService"></bean> </beans>
获取实例化对象
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); UserService userService = (UserService) ac.getBean("userService"); userService.test();
当咱们指定Spring使用静态工厂方法来建立Bean实例时,Spring将先解析配置文件,并根据配置文件指定的信息,经过反射调用静态工厂类的静态工厂方法,并将该静态工厂方法的返回值做为Bean实例,在这个过程当中,Spring再也不负责建立Bean实例,Bean实例是由用户提供的静态工厂方法提供的。
注:
定义工厂类
package com.xxxx.factory; import com.xxxx.service.UserService; /** * 定义工厂类 */ public class InstanceFactory { /** * 定义方法,返回实例化对象 * @return */ public UserService createUserService() { return new UserService(); } }
设置配置文件 spring.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"> <!-- 实例化工厂 1.定义实例化工厂bean 2.引用工厂bean 指定工厂建立方法(方法为非静态) --> <bean id="instanceFactory" class="com.xxxx.factory.InstanceFactory"></bean> <bean id="userService" factory-bean="instanceFactory" factory-method="createUserService"></bean> </beans>
获取实例化对象
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml"); UserService userService = (UserService) ac.getBean("userService"); userService.test();
方式三:利用实例化factory方法建立,即将factory方法也做为了业务bean来控制,1可用于集成其余框架的bean建立管理方法,2可以使bean和factory的角色互换。
开发中项目通常使用一种方式实例化bean,项目开发基本采用第一种方式,交给Spring托管,使用时直接拿来使用便可。另外两种了解