1.静态工厂创建对象
bean类
public class StaticHouse {
private static Map<String,House> map;
static{
map = new HashMap<String,House>();
map.put("lodon",new House("lodon",5000000.0));
map.put("jjj",new House("jjj",451222.0));
map.put("aegre",new House("pro",2222.0));
}
public StaticHouse(Map<String, House> map) {
this.map = map;
}
public static House getHouse(String name) {
return map.get(name);
}
public Map<String, House> getHouse() {
return map;
}
}
配置文件
<bean id="static" class="com.oracle.factory.StaticHouse" factory-method="getHouse">
<constructor-arg value="jjj"></constructor-arg>
</bean>
constructor-arg =get方法里面传的参数
2.实例工厂方法
别的和上一个都一样,就是把静态去掉,赋值在构造器里
public class Factoru {
private Map<betring,Hose> map;
public FactoryHouse(){
map = new HashMap<String,House>();
map.put("lodon",new House("lodon",5000000.0));
map.put("jjj",new House("jjj",451222.0));
map.put("aegre",new House("pro",2222.0));
}
public House getHouse(String name) {
return map.get(name);
}
}
配置文件是这么写的:
<bean id="factory" class="com.oracle.factory.FactoryHouse"></bean>
<!--先写一个普通的bean,在再另一个里面调用-->
<bean id="factoryGet" factory-bean="factory" factory-method="getHouse">
<constructor-arg value="lodon"></constructor-arg>
</bean>
测试代码,没什么好写的
@Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
House house = (House)ac.getBean("static");
System.out.println(house);
}
@Test
public void testFactory(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
House house = (House)ac.getBean("factoryGet");
System.out.println(house);
}
注解部分
@Component 写完这个就相当于写了个bean
默认的bean id是类名首字母小写
这个东西要在配置文件里面加上范围
<context:component-scan base-package="com.oracle.annotation.bean"/>
@Scope 修改是否是单例模式,默认就不是
@Scope("singleton")是单例模式
@Scope("property")是多例模式(好像是这个)
@Controller 控制器组件 :这是springMVC的C部分
MVC(Model View Controller) 模型视图控制
@Service 服务层组建 :这个写在service层里,就是个标志
@Respositor :数据访问层,是dao用的注释
@Component:对象层 bean
resource-patten属性是用来过滤哪个注释不执行的
<contex:component-scan base-package="com.oracle.annotation" resource-pattern="service/*.class"/>
自动装配
@AutoWired:写完这个之后自动为你实力一个对象
private String name;
可以给字段写,也可以给方法写
@AutoWired
1. public void setPersonService(PersonService personService){ //这就是随便写的,不用介意
2. this.personService = personService;
3. System.out.println("为personService创建对象");
4. System.out.println("personService:"+personService);
5. }
@Qualifer("什么什么") :注入某个bean