【初识Spring】对象(Bean)实例化及属性注入(xml方式)
2020-12-13 17:00
标签:single oid 对象 port 加载 entry xmla let ica title: 【初识Spring】对象(Bean)实例化及属性注入(xml方式) ?#初识Spring之Bean实例化及属性注入 1.通过xml配置的方式进行实例化。 2.属性注入。 (1)id属性:起名称,id属性值名称任意命名 (2)class属性:创建对象所在类的全路径 (3)name属性:功能和id属性一样的,id属性值不能包含特殊符号,但是在name属性值里面可以包含特殊符号 (4)scope属性 prototype:多例 无参构造实例化对象 注:java类中默认有无参构造方法,若类中已声明了有参构造,则需手动声明无参构造方法。 Phone类改写为: applicationContext.xml配置文件修改为: 测试类: 结果: Phone类改写为: applicationContext.xml配置文件修改为: 结果: 新建Person类: Phone类修改为: 配置文件作如下修改: 测试方法不变,结果为: Phone类修改为: 配置文件作如下修改: 结果如下: 【初识Spring】对象(Bean)实例化及属性注入(xml方式) 标签:single oid 对象 port 加载 entry xmla let ica 原文地址:https://www.cnblogs.com/flytree/p/11622685.html
date: 2018-08-29 17:35:15
tags: [Java,Web,Spring]
---
xml配置的方式进行实例化
//phone类:
package com.test.vo;
public class Phone {
public void printTest() {
System.out.print("Phone.......");
}
}
//测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//加载配置文件,创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//得到配置创建的对象
Phone Phone = (Phone) context.getBean("phone");
//调用对象方法
Phone.printTest();
}
}
//静态工厂类
package com.test.utils;
import com.test.vo.Phone;
public class BeanFactory {
//静态方法,返回Phone对象
public static Phone getPhone() {
return new Phone();
}
}
//创建的对象为Phone类对象不变
//配置文件改为:
//测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Phone Phone = (Phone) context.getBean("phoneFa");
Phone.printTest();
}
}
//实列工厂类:
import com.test.vo.Phone;
public class BeanUFactory {
//普通方法,返回Phone对象
public Phone getPhone() {
return new Phone();
}
}
配置文件修改:
//测试类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Phone Phone = (Phone) context.getBean("phoneUFa");
Phone.printTest();
}
}
属性注入
public class Phone {
private String name;
//显示声明无参构造
public Phone() {}
//有参构造
public Phone(String name) {
this.name=name;
}
public void printTest() {
System.out.print(name+"Phone.......");
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Phone Phone = (Phone) context.getBean("phoneFa");
Phone.printTest();
}
}
诺基亚Phone.......
public class Phone {
private String name;
//set方法
public void setName(String name) {
this.name = name;
}
public void printTest() {
System.out.print(name+"Phone.......");
}
}
三星Phone.......
public class Person {
private String name;
private String sex;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
package com.test.vo;
public class Phone {
private String name;
private Person person;
//set方法
public void setName(String name) {
this.name = name;
}
public void setPerson(Person person) {
this.person = person;
}
public void printTest() {
System.out.print(person.getName()+"::"+person.getAge()+"::"+person.getSex());
}
}
小王::11::man
package com.test.vo;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class Phone {
private String arr[];
private List
arr:[小米, 中兴, 华为]
list:[1, 2, 3]
map:{aa=lucy, bb=bob, cc=jerry}
文章标题:【初识Spring】对象(Bean)实例化及属性注入(xml方式)
文章链接:http://soscw.com/index.php/essay/36650.html