通过工厂方式配置bean
src\dayday\CarFactoryBean.java
package dayday; import org.springframework.beans.factory.FactoryBean; /**
* Created by I am master on 2016/11/30.
*/
//自定义的FactoryBean需要实现FactoryBean接口
public class CarFactoryBean implements FactoryBean<Car>{
private String brand;
public void setBrand(String brand){
this.brand=brand;
}
@Override
public Car getObject() throws Exception {
return new Car(brand,"50w");
} @Override
public Class<?> getObjectType() {
return Car.class;
} @Override
public boolean isSingleton() {
return true;
}
}
src\dayday\Car.java
package dayday; import com.sun.org.apache.xpath.internal.SourceTree; import javax.sound.midi.Soundbank; /**
* Created by I am master on 2016/11/28.
*/
public class Car {
private String name;
private String money;
public void setName(String name){
this.name=name;
System.out.println("setName...");
}
public String getName(){
return name;
}
public void setMoney(String money){
this.money=money;
}
public String getMoney(){
return money;
}
public String toString(){
return "Car"+"["+"carname="+name+" carValue="+money+"]";
}
public Car(String name,String money){
this.money=money;
this.name=name;
}
public Car(){
System.out.println("Car's constructor...");
}
public void init(){
System.out.println("init...");
}
public void destroy(){
System.out.println("destroy...");
}
}
src\dayday\Main.java
package dayday; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.SQLException; /**
* Created by I am master on 2016/11/28.
*/
public class Main {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
Car car=ctx.getBean("car",Car.class);
System.out.println(car);
}
}
src\beans.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:p="http://www.springframework.org/schema/p"
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.xsd">
<!--
通过FactoryBean来配置Bean的实例
class指向FactoryBean的全类名
property:配置FactoryBean的属性,但实际返回的实例是FactoryBean的getObject()方法返回的实例
-->
<bean id="car" class="dayday.CarFactoryBean">
<property name="brand" value="BMW"></property>
</bean>
</beans> 运行结果
Car[carname=BMWcarValue=50w]
通过工厂方式配置bean的更多相关文章
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- spring4-2-bean配置-9-通过工厂方法配置bean
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjwAAAFICAIAAADbNrOHAAAgAElEQVR4nO2dy7W0uhGFOxWn4JEXAS
- 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)
组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...
- spring学习笔记 星球日two - 注解方式配置bean
注解要放在要注解的对象的上方 @Autowired private Category category; <?xml version="1.0" encoding=" ...
- spring学习笔记 星球日one - xml方式配置bean
ide: idea lib包的导入:http://webcache.googleusercontent.com/search?q=cache:http://zyjustin9.iteye.com/bl ...
- 4.spriing:Bean的生命周期/工厂方法配置Bean/FactoryBean
1.Bean的生命周期 scope:singleton/prototype 1)spring容器管理singleton作用的生命周期,spring能够精确知道Bean合适创建,何时初始化完成,以及何时 ...
- Spring框架学习(6)使用ioc注解方式配置bean
内容源自:使用ioc注解方式配置bean context层 : 上下文环境/容器环境 applicationContext.xml 1 ioc注解功能 注解 简化xml文件配置 如 hibernate ...
- Spring初学之通过工厂方法配置Bean
工厂方法配置bean分为两种,一种是实例工厂方法,另一种是静态工厂方法. 先来看看实体bean: Car.java: package spring.beans.factory; public clas ...
- 11.Spring通过工厂方法配置Bean
通过工厂方法配置Bean暴扣静态工厂方法和实例工厂方法. 1.静态工厂方法 调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中,当客户端需要对象时,只需要简单的调用静态方法,而不去关心创建 ...
随机推荐
- appml的安装与使用
来自w3cshool的中文文档:http://www.w3cschool.cc/appml/appml-tutorial.html 我们要做的是完整的例子,所以从这里http://www.w3csch ...
- ActiveReports中自定义Winforms的Viewer 工具栏
ActiveReports中提供不同平台的报表浏览器来加载报表,而不同平台的报表浏览器功能也不一致,今天我们来学习如何定制Winforms Viewer控件. 预览效果: 核心代码: C# // C# ...
- 【Delphi7】 解决“程序第一次可以正常编译,但再次编译的时候会报错,必须重新打开Delphi”的问题
报错如下: Access violation at address 00495044 in module 'coreide70.bpl'. Read of address...Access viola ...
- 深入剖析tomcat 笔记——第8章 载入器
深入剖析tomcat 笔记 目录:
- xtrapivotcontrol 控件用法及相关属性
XtraPivotControl使用指南:可参照以下的文档: 转载自:http://wenku.baidu.com/view/d7886b20aaea998fcc220e53.html 上面链接中对x ...
- markdown语法书
因为初用markdown,所以对它的语法还不是很熟悉.喜欢简书的风格,特地拷贝了一份markdown语法手册,可以实现效果立显. http://www.jianshu.com/writer#/note ...
- PC端和移动端一些奇葩兼容性问题
IE10默认在input框中输入内容时会显示一个'X',密码框会显示一个'小眼睛',怎么把这个默认的'X'或者'小眼睛'删除掉. 在password输入框显示一个“小眼睛”的按钮,去掉他的方法如下: ...
- javascript数组的方法总结,非常实用的!
在ES5中,一共有9个Array方法 http://kangax.github.io/compat-table/es5/ 注* 九个方法 Array.prototype.indexOfArray.pr ...
- how to get soul shields in blade and soul
These soul shields can either be obtained by E.Fleet Supply Chain or Blackram Supply Chain (4-man or ...
- umask
1. 首先我们来思考umask是什么? umask 是系统设置的权限的默认值,在etc/profile里面的shell 脚本有设置规则. 对于root用户和用户而言,不可以直接用的 需要用减法 比如 ...