1.懒加载
理解懒加载:
懒加载是建立在初始化上的,首先要在xml文件中配置Bean时要配合指定nit-method(),来显示出来
然后这个Bean还是单例的指定scope="singleton"(不指定默认也是单例的).这时候
就会在加载main方法时就会自动的初始化实例化对象,当在<Bean>里指定属性lazy-init("true")
时,就不会在加载时自动实例化对象,这个方式就时懒加载.
懒加载的优点缺点:
懒加载:对象使用的时候才去创建,节省资源,但是不利于提前发现错误。
非懒加载:容器启动的时候立刻创建对象。消耗资源。利于提前发现错误。
当scope=“prototype” (多例)时,默认以懒加载的方式产生对象。
当scope=“singleton” (单例)时,默认以非懒加载的方式产生对象。
2.bean的name属性
name属性跟Bean里的id相似可以设置多个值用逗号隔开,例如
里面的每个值都可以做限定
3.注解配置导入xml配置
注解的这种方式骚操作,首先的肯定得会使用注解,这个详情见我的另一篇文章
这里就先说一下这种方式
@ImportResource("aaa.xml")这就ok了
4.依赖注入(DI)
注入匿名内部Bean
就是创建一个匿名的Bean对象,只能在这个对象里面使用,其他的不能使用.类似匿名内部类
在xml文件中的Beans标签里给Stuedent注入一个匿名对象用 property属性
Fruit接口里面有个吃水果的方法
Apple里面实现了Fruit的方法
Fruit是Student里面的一个属性
<bean id="stu" class="com.aaa.spring.bean.Student">
<property name="fruit">
<bean class="com.aaa.spring.simple.Apple" />
</property>
</bean>
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Student stu = ctx.getBean("stu",Student.class);
stu.getFruit().eatFruit();
注入集合类型属性 list
创建一个Person类
package com.aaa.spring.bean;
public class Person {
private String sname;
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
}
在xml配置
<bean id="person" class="com.aaa.spring.bean.Person">
<property name="sname">
<list>
<value>张三</value>
<value>李四</value>
<value>王五</value>
</list>
</property>
</bean>
main方法里面
Person person = ctx.getBean("person",Person.class);
List<String> list = person.getSname();
System.out.println(list);
map跟这个是类似的可以存放键值对
看一下在xml里面是则么配置的就不详细写了
<property name="score">
<map>
<entry key="语文" value="100"></entry>
<entry key="数学" value="120"></entry>
</map>
</property>
跟list其他都是很类似的 并且 在 他们都是可以指定数据类型的
注入空字符串类型的值时就是在<property name= ""> <null/> </property>这样就可以了
5.注入复合属性值
什么是复合属性值,简单来说就是把另一个类中的属性给注入进来也可以说是关联
以上面list里面的示例来演示一下复合属性的用法
在创建一个Teacher类
public class Teacher{
private List<String> subject;
public void getSubject(){
return this.subject;
}
public void set Subject(){
this.subject = subject;
}
}
把Teacher注入到 Person类中 在xml里面配置
<bean id="person" class="com.aaa.spring.bean.Person">
<property name="sname">
<list>
<value>张三</value>
<value>李四</value>
<value>王五</value>
</list>
</property>
<property name = "teacher">
<bean class = "com.aaa.spring.bean.Teacher"></bean>
</property>
<property name="teacher.subject" value="语文" />
</bean>
在main 方法中调用
person.getTeacher().getSubject();
6.注入外部properties文件中的属性值
创建一个文件jdbc.properties内容也就是我们连接书库时的必须属性
jdbc.url=jdbc:mysql://localhost:3306/lanou?charsetEncoding=utf8
jdbc.driver=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=root
在xml中配置有两种方式 推荐方式one
方式one
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
方式two
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="jdbcConf" class="com.aaa.spring.bean.JDBCConf">
<property name="url" value="${jdbc.url}" />
<property name="driver" value="${jdbc.driver}" />
<property name="userName" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean>
使用创建一个类 创建四个对应的属性 生成getter setter方法
在mian方法中使用就可以了哦
觉着我代码写的不全的小伙伴可要理解我哦 写的不全可以根据我的文字描述自己去思考使用哦,肯定比全是代码的事半功倍,不要感谢我!!! | |
---|---|