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

31次阅读

共计 3690 个字符,预计需要花费 10 分钟才能阅读完成。

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 注解标注在属性上就能够实现主动拆卸了

正文完
 0