spring 其他bean的引用 02

先添加jar包

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
        <!--spring-core-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <!--spring-bean-->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <!--spring-context-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <!--spring-expression-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
        </dependency>
    </dependencies>

第二步创建配置文件applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  
</beans>

第三步书写一个类User

package com.ioc06.entity;

import java.net.PasswordAuthentication;

/**
 * package_name:com.ioc06.entity
 * Author:徐亚远
 * Date:2020/1/19 19:00
 * 项目名:springDemo01
 * Desription:
 **/
public class User {
    //其他bean的引用
    private String username;
    private String password;


    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

第四步书写一个类UserLogin

package com.ioc06.entity;

import org.springframework.aop.target.LazyInitTargetSource;

import java.util.*;

/**
 * package_name:com.ioc06.entity
 * Author:徐亚远
 * Date:2020/1/19 19:02
 * 项目名:springDemo01
 * Desription:
 **/
public class UserLogin {
    //其他bean的引用
    private User user;
   @Override
    public String toString() {
        return "UserLogin{" +
                "user=" + user +
                '}';
    }

   public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

配置spring,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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="user" class="com.ioc06.entity.User">
      <property name="username" value="admin"/>
      <property name="password" value="admin"/>
  </bean>
    <bean id="userLogin" class="com.ioc06.entity.UserLogin">
         <!--其他bean的引用-->
        <property name="user" ref="user"/> 
        </property>
  
    </bean>
</beans>

测试UserController类

package com.ioc06.controller;

import com.ioc06.entity.UserLogin;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * package_name:com.ioc06.controller
 * Author:徐亚远
 * Date:2020/1/19 19:03
 * 项目名:springDemo01
 * Desription:
 **/
public class UserLoginController {
    public static void main(String args []){
        ApplicationContext ac = new ClassPathXmlApplicationContext("ioc06/spring.xml");
       UserLogin userLogin = (UserLogin) ac.getBean("userLogin");
        System.out.println("username:"+"     "+userLogin.getUser().getUsername());
        System.out.println("password"+"      "+userLogin.getUser().getPassword());
        System.out.println("userLogin:"+" "+userLogin);
    }
}

测试结果如图:
在这里插入图片描述
目录结构如图:
在这里插入图片描述

发布了64 篇原创文章 · 获赞 1 · 访问量 922

猜你喜欢

转载自blog.csdn.net/weixin_43311650/article/details/104044669
今日推荐