学习Spring笔记--Ioc
IOC概念: Inversion of Control 控制反转或反向控制
控制反转:改变了对象获取方式,之前编码方式采用new构造器方式获取对象;ioc中采用由容器创建对象之后注入进来使用。\
一、SpringIOC步骤
1、 添加SpringIoc环境
--引入jar包
--src/applicationContext.xml
2、在applicationContext.xml中定义<bean>
<bean id="" class="包名,类" >
3、实例化Spring容器ApplicationContext
String conf="applicationContext.xml";
ApplicationContext ac= new ClassPathXmlApplicationContext(conf);
4、使用时通过getBean(“id值”)方法提取
Calendar c1 = ac.getBean("c1",Calendar.class); //用到格式转变
二、SpringIoc应用
a、管理对象
创建、初始化、释放资源、销毁
b、维护对象关系
采用注入方式建立对象关系
Dependency Injection (DI)依赖注入
依赖注入:set方法注入,构造器注入
c、搭建SpringIOC开发环境
---引入相关jar包
---在src添加applicationContext.xml
*Spring创建Bean对象的控制
1、控制对象创建方式(使用范围)
在<bean>元素中使用scope属性配置,默认值是singleton
<bean scope="singleton">该组件在Spring容器里只有一个bean对象
<bean scope=“prototype”>该组件每次ac.getbean("id");都返回一个新的对象
2、指定对象初始化方法
利用<bean>元素的init-method指定
当创建对象后自动执行init-method方法
3、指定对象销毁方法
利用<bean>元素的destroy-method指定。满足下面条件才有效
---组件对象为singleton单例模式
---调用AbstractApplicationContext容器对象的close()方法
4、控制单例对象创建时机
在默认情况下,单例对象是Spring容器创建时实例化;可以使用<bean>元素的lazy-init=true 属性将创建时机推迟到getbean()方法调用时。
信息注入
1、set信息注入使用《property》
2、构造器注入使用《constructor-arg》
3、自动注入
《bean autowire=“”》 用于指定自动注入规则,可以使用bytype,byname,constructor。用于简化注入配置。
使用bytype类型匹配注入需要注意,有2个及其以上匹配会出异常。
4、各种类型信息的注入配置格式
a、注入字符串。数值单个数值
<property name=" " value=" ">
b、注入bean对象
c、注入集合list,set,map,properties
* list<String> list
<property>
<list><value></value> </list>
</property>
* Map<String,String> books
<property name="books">
<map> <entry key=“1001” value="java">
<entry key=“1002” value="java web"> </map>
</property>
在class文件中: Set<Entry<String, String>> booksSet=books.entrySet();
for(Entry<String,String> e: booksSet){
System.out.println(e.getKey()+":"+e.getValue());
}
* Properties db;
<property name="db">
<props>
<prop key="username"> root</prop>
<prop key="password"> 123456</prop>
<prop key="driver"> com.mysql.jdbc.Driver</prop>
</props>
</property>
在class文件中Set<Object> keys = db.keySet();
for(Object key : keys){
System.out.println(key+":"+db.getProperty(key.toString()));
}
d、Spring表达式注入
#{表达式}
#{id名.属性}或#{id名.key}
如果是对象属性,需要有getXXX方法
===============================================================================
a.组件自动扫描
可以按指定的包路径,将包下所有组件扫描,如果发现组件类定义前有以下标记,会将组件扫描到Spring容器中
@Component //其他组件 @Controller //控制层组件
@Service //业务层组件 xxxService @Repository//数据访问层组件 xxxDao @Named(需要引入第三方标准包)
* 扫描之后相当于自动配置,省去<bean id="" class=" "/>步骤
*如果要给其添加Scope属性,则在@Comp
onent下一行添加@Scope("prototype"),等价于<bean scope="">
*@PostConstruct //等价于<bean init-method="">
public void init(){ System.out.println("初始化逻辑") }
*@PreDestroy //等价于<bean destroy-method="">
public void destroy(){System.out.println("释放资源") }
一般默认ID名为其类名的首字母小写,如果想要自定义名字,则需@Component("exam1") exam1就是自定义名字
b、注入
1、对对象的注入
@Resource 位于javax 可以在变量定义前或set方法定义前应用
@AutoWired 位于Spring 可以在变量定义前或在SET方法前应用
一般使用时,功能等价,都可以实现注入。
如果不存在多个匹配类型,使用@Reasouse或@Autowired都可以。如果存在多个匹配类型,建议按名称注入@Resourse(name="指定名称")或@Autowired 换行 @Qualifier(“p”)
使用建议:set注入建议用@Resource 构造器建议用Autowired
SpringIOC应用:自己编写的组件建议使用注解配置,框架API只能用XML配置
2、对单个数据的注入
在applicationContext.xml文件中写<util:properties id=" "><prop key=" ">xxx</prop>.....</utilLproperties>
然后在class文件的数据定义之前加,如:@Value("#{db.username}") private String username