文章目录
- 一、作用域
- 二、延迟加载
- 三、类型装配
- 四、包扫描的方式创建bean
一、作用域
- 容器内部默认使用的是单例模式
- 两种模式
- 单例模式
<bean id ="SomeService" class="org.example.SomeService" scope="singleton" ></bean> # 备注# scope= "singleton" 单例模式,默认# scope= "prototype" 单例模式,默认
- 多例模式
<bean id ="SomeService" class="org.example.SomeService" scope="prototype" ></bean>
二、延迟加载
<bean id ="SomeService" class="org.example.SomeService" scope="prototype" lazy-init="true"></bean># 备注# lazy-init="true" 什么时候用对象,什么时候创建对象
三、类型装配
``` - 接口类 ``` package org.example;
import java.util.List;public interface ISomeService {void doSome(); }```
- 实现类
package org.example;import java.util.List;public class SomeService implements ISomeService{private String name;public SomeService (String name){this.name = name;}@Overridepublic void doSome() {System.out.println("SomeService ----name值:"+name);}}
- main
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");ISomeService iSomeService = context.getBean("SomeService",ISomeService.class); iSomeService.doSome();
- 引用类型装配
- application.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 ="SomeService" class="org.example.SomeService" scope="prototype" lazy-init="true"><property name="lists"><list><value>1</value><value>2</value><value>3</value><value>4</value></list></property> </bean></beans>
- 接口类
package org.example;import java.util.List;public interface ISomeService { List<String> getList();}
- 实现类
package org.example;import java.util.List;public class SomeService implements ISomeService{private List<String> lists; public void setLists(List<String> lists) {this.lists = lists;}@Overridepublic List<String> getList() {return lists;}}
- main
package org.example;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {/*** main* @param args*/public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");ISomeService iSomeService = context.getBean("SomeService",ISomeService.class);System.out.println(iSomeService.getList()); }}
四、包扫描的方式创建bean
- applicationContextScanning.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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.scanningPackage"/></beans>
- 接口类
package com.scanningPackage;public interface ISomeService {void doSome();}
- 实现类
package com.scanningPackage;import org.springframework.stereotype.Service;@Servicepublic class SomeService implements ISomeService{@Overridepublic void doSome() {System.out.println("SomeService--doSome");}}
- 注解注入
- @Repository
- @Service
- @Controller
- @Component
- @Autowired [spring 框架提供的]
- @Resource [java 提供]
- main
package com.scanningPackage;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {private static ISomeService iSomeService;public static void main(String[] agrs) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextScanning.xml");iSomeService = context.getBean("someService",ISomeService.class);iSomeService.doSome();}}