Bean 的属性:
1、class:是强制的,用来创建指定bean的bean类。*(路径为包名.类名)
id:名字。*(唯一性)
2、name:用来指定标识符。*(需与创建类变量保持一致)
3、scop:是特定的bean定义创建对象的作用域。*(如public)
**4**、constructor-arg:他是依赖注入里的构造器注入里的一个标签。
**5**、properties:用于bean与bean的连接,以及设置属性。
**6**、autowiring mode:用于bean的setter和非setter方法构造函数和属性。
4、5、6用于注入依赖关系。
Bean的作用域:
1、singleton 将bean的定义的限制在每一个springIOC中,——单一实例。
2、prototype将单一bean的定义在任意数量的对象实例,——多项实例。
简单代码操作:
- 简单方法类 :A01
package FxBeanJBCZ01;
import java.text.DecimalFormat;
import java.text.Format;
public class A01 {
public void aa(){
int aa=01;
Format a=new DecimalFormat(“00”);
//这个方法就是可以吧int类型01给在控制台打印出来
System.out.println(a.format(01));
}
}
- 第二 测试类: Bsyso
package FxBeanJBCZ01;
import java.text.DecimalFormat;
import java.text.Format;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Bsyso {
public A01 aa;
public A01 getAa() {
return aa;
}
public void setAa(A01 aa) {
this.aa = aa;
}
public static void main(String[] args) {
ApplicationContext aa=new ClassPathXmlApplicationContext(“fxbean01.xml”);
//创建容器对象并指定xml文件
Bsyso wc=(Bsyso) aa.getBean(“d”);
//对象aa调用getbean方法指定bean赋值给wc。
wc.aa.aa();
//对象wc调用aa类变量aa的类方法aa();
}
}
- xml文件配置:
需要导入一些jar包
E:\STS\JAR包\commons-logging-1.2.jar
E:\STS\JAR包\spring-aop-4.3.5.RELEASE.jar
E:\STS\JAR包\spring-beans-4.3.5.RELEASE.jar
E:\STS\JAR包\spring-context-4.3.5.RELEASE.jar
E:\STS\JAR包\spring-core-4.3.5.RELEASE.jar
E:\STS\JAR包\spring-expression-4.3.5.RELEASE.jar
E:\STS\JAR包\spring-web-4.3.5.RELEASE.jar
E:\STS\JAR包\spring-webmvc-4.3.5.RELEASE.jar
其中只用到beans context两个其余的在以后用到
< ?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd”>
< bean id=“a” class=“FxBeanJBCZ01.A01”>< /bean>
< bean id=“d” class=“FxBeanJBCZ01.Bsyso”>
< property name=“aa” ref=“a”>< /property>
< /bean>
< /beans>