关于后端:还不懂spring中的bean的话你一定得好好看看这篇文章

bean的作用域

bean的生命周期

bean的拆卸

代码

实体类

package com;

import java.util.List;

public class User {

    private String username;
    private String password;
    private List<String> list;
    /**
     * 设值注入要求bean类:
     * 1、必须提供默认无参构造方法
     * 2、为属性提供setter办法
     */
    public User() {
    }

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

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String 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 +
                '}';
    }

}

dao层

package com;

import org.springframework.stereotype.Repository;

@Repository("userDao1")
public class UserDaoImpl implements UserDao{
    public void say() {
        System.out.println("dao");
    } 
}

service层

package com;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("userService")
public class UserServiceImpl implements UserService{

    @Resource(name="userDao1")
//    @Autowired
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void save() {
        this.userDao.say();
        System.out.println("service");
    }
}

controller层

package com;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

@Controller("userController")
public class MyController {

    @Resource(name="userService")
//    @Autowired
    UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void say(){
        this.userService.save();
        System.out.println("controller");
    }
}

基于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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-4.3.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

<bean id="userDao" class="com.UserDaoImpl"/>

<!-- 应用结构注入形式拆卸bean   -->
<bean id="user1" class="com.User">
    <constructor-arg index="0" value="zhangsan" />
    <constructor-arg index="1" value="123" />
    <constructor-arg index="2">
        <list>
            <value>construct1</value>
            <value>construct2</value>
        </list>
    </constructor-arg>
</bean>
    <!-- 应用设值注入形式拆卸bean   -->
<bean id="user2" class="com.User">
        <property name="username" value="lisi" />
        <property name="password" value="123" />
        <property name="list">
            <list>
                <value>set1</value>
                <value>set2</value>
            </list>
        </property>

    </bean>
</beans>

基于Annotation拆卸

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-4.3.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

     <bean id="userDao1" class="com.UserDaoImpl" />
    <bean id="userService" class="com.UserServiceImpl" />
    <bean id="userController" class="com.MyController" />
        <!-- 开启注解   -->
    <context:annotation-config />
</beans>

主动拆卸

<!--       扫描包-->
 <context:component-scan base-package="com"/>

小结

而后用@Autowired注解标注在属性上就能够实现主动拆卸了

【腾讯云】轻量 2核2G4M,首年65元

阿里云限时活动-云数据库 RDS MySQL  1核2G配置 1.88/月 速抢

本文由乐趣区整理发布,转载请注明出处,谢谢。

您可能还喜欢...

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据