码迷,mamicode.com
首页 > 其他好文 > 详细

Bean装配方式

时间:2019-03-04 21:03:59      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:one   bean   location   his   span   return   list   etl   users   

一、基于XML的装配

(1)构造注入方式装配

   User类:

public class User {
    private String username;
    private Integer password;
    private List<String> list;

    public User(){

    }

    public User(String username, Integer password, List<String> list) {
        this.username = username;
        this.password = password;
        this.list = list;
    }

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

    Test类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("com/chauncey/ioc/beans.xml");
        User user = (User) context.getBean("userOne");
        System.out.println(user);
    }
}

   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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
  <bean id="userOne" class="com.chauncey.ioc.User">
      <constructor-arg index="0" value="小白"/>
      <constructor-arg index="1" value="123456"/>
      <constructor-arg index="2">
          <list>
              <value>"我是构造注入方式"</value>
              <value>"我是构造注入方式"</value>
          </list>
      </constructor-arg>
  </bean>
</beans>

   运行结果:

 技术图片

(2)设值注入方式装配

   User类:

public class User {
    private String username;
    private Integer password;
    private List<String> list;

    public User(){

    }

    public String getUsername() {
        return username;
    }

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

    public Integer getPassword() {
        return password;
    }

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

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

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

   beans.xml:

   <bean id="userTwo" class="com.chauncey.ioc.User">
        <property name="username" value="小灰"/>
        <property name="password" value="123456"/>
        <property name="list">
            <list>
                <value>"我是设值注入方式"</value>
                <value>"我是设值注入方式"</value>
            </list>
        </property>
    </bean>

   运行结果:

技术图片

 

二、基于注解

   创建UserDao接口和UserService接口,在两个中添加save方法,创建两个接口的实现类UserDaoImpl和UserServiceImpl

   UserDaoImpl接口:

@Repository("userDao")
public class UserDaoImpl implements UserDao {
    @Override
    public void save() {
        System.out.println("UserDao...Save...");
    }
}

   UserServiceImpl:

@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource(name = "userDao")
    private UserDao userDao;
    @Override
    public void save() {
        userDao.save();
        System.out.println("UserService...Save...");
    }
}

        UserController:

@Controller("userController")
public class UserController {
    @Resource(name = "userService")
    private UserService userService;

    public void save() {
        userService.save();
        System.out.println("Controller...Save...");
    }
}

       Test类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("com/chauncey/user/bean.xml");
        UserController controller = (UserController) context.getBean("userController");
        controller.save();
    }
}

    bean.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: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">

    <!--对包下的bean进行扫描-->
    <context:component-scan base-package="com.chauncey.user"/>
</beans>

 

Bean装配方式

标签:one   bean   location   his   span   return   list   etl   users   

原文地址:https://www.cnblogs.com/chaunceyji/p/10472884.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!