SpringIOC创建对象的三种方式

野性酷女 2022-11-25 00:37 328阅读 0赞

1、默认使用无参构造的创建对象

Hello.java:
  1. public class Hello {
  2. private String name;
  3. public void setName(String name) {
  4. this.name = name;
  5. }
  6. public void show(){
  7. System.out.println("Hello "+this.name);
  8. }
  9. }
ApplicationContext.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  3. <bean id="hello" class="cn.com.Ycy.spring.domain.Hello">
  4. <property name="name" value="name"/>
  5. </bean>
  6. </beans>
测试类:
  1. @Test
  2. public void test02(){
  3. ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
  4. Hello hello = (Hello) ac.getBean("hello");
  5. hello.show();
  6. }
如果写了有参构造方法(没写无参构造方法),这里的class就会爆红,类中没有无参构造方法,但是spring要调用无参构造方法。

2、我们要使用有参构造方法创建对象

1. 下标赋值

ApplicationContext.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  3. <bean id="hello" class="cn.com.Ycy.spring.domain.Hello">
  4. <constructor-arg index="0" value="java"/>
  5. </bean>
  6. </beans>

2. 类型赋值(不好用,有两个相同的类型就会出现问题)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  3. <bean id="hello" class="cn.com.Ycy.spring.domain.Hello">
  4. <constructor-arg type="java.lang.String" value="java"/>
  5. </bean>
  6. </beans>

3. 参数名name赋值

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  3. <bean id="hello" class="cn.com.Ycy.spring.domain.Hello">
  4. <constructor-arg name="name" value="spring"/>
  5. </bean>
  6. </beans>

3、使用普通工厂中的方法创建对象

使用某个类中的方法创建对象,并存入spring容器
接口:

  1. public interface IAccountDao {
  2. /** * 模拟保存账户 */
  3. void saveAccount();
  4. }

接口实现类:

  1. public class AccountDaoImpl implements IAccountDao {
  2. public AccountDaoImpl(){
  3. System.out.println("AccountDaoIMpl对象创建了");
  4. }
  5. public void saveAccount(){
  6. System.out.println("保存了账户");
  7. }
  8. }

普通工厂类:

  1. public class beanFactory {
  2. //工厂类-创建对象
  3. public IAccountDao getAccountDao(){
  4. return new AccountDaoImpl();
  5. }
  6. }

bean配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  3. <bean id="beanfactory" class="com.Ycy.factory.beanFactory"></bean>-->
  4. <bean id="accountDao" factory-bean="beanfactory" factory-method="getAccountDao"></bean>
  5. </beans>

测试方法:

  1. @Test
  2. public void test01(){
  3. ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
  4. IAccountDao accountDao = (IAccountDao) ac.getBean("accountDao");
  5. System.out.println(accountDao);
  6. accountDao.saveAccount();
  7. }

4、使用工厂中的静态方法创建对象

使用某个类中的静态方法创建对象,并存入spring容器
工厂类:

  1. public class staticFactory {
  2. public static IAccountDao getAccountDao(){
  3. return new AccountDaoImpl();
  4. }
  5. }

接口:

  1. public interface IAccountDao {
  2. /** * 模拟保存账户 */
  3. void saveAccount();
  4. }

接口实现类:

  1. public class AccountDaoImpl implements IAccountDao {
  2. public AccountDaoImpl(){
  3. System.out.println("AccountDaoIMpl对象创建了");
  4. }
  5. public void saveAccount(){
  6. System.out.println("保存了账户");
  7. }
  8. }

bean配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  3. <bean id="accountDaoByStatic" class="com.Ycy.factory.staticFactory" factory-method="getAccountDao"></bean>
  4. </beans>

测试方法:

  1. /** * 静态工厂创建对象 */
  2. @Test
  3. public void test02(){
  4. ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
  5. IAccountDao accountDao = (IAccountDao) ac.getBean("accountDaoByStatic");
  6. System.out.println(accountDao);
  7. accountDao.saveAccount();
  8. }
创建对象

类型 变量名 = new 类型();
Hello hello = new Hello();
id = 变量名
class = new 的对象
property 相当于给对象中的属性设置一个值 —>对应的是set方法
没有set对应的方法是会报错的
在配置文件加载时,容器中的所有对象已经初始化了

发表评论

表情:
评论列表 (有 0 条评论,328人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring--对象创建方式

    第一种方式:空参构造创建对象 最基本的对象创建方式,只需要有一个无参构造函数和字段的set方法。本质上就是使用无参构造器创建对象,然后使用set方法为字段赋值。 Us