欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring中的bean

程序员文章站 2022-08-13 20:34:12
文章目录bean的作用域bean的生命周期bean的装配代码实体类dao层service层controller层基于XML装配(设值注入和构造注入)基于Annotation装配自动装配bean的作用域作用域描述singleton(单例)在spring IoC容器仅存在一个Bean实例,Bean以单例方式存在,bean作用域范围的默认值。request每次HTTP请求都会创建一个新的Bean,该作用域仅适用于web的Spring WebApplicationContext环境...

bean的作用域

作用域 描述
singleton(单例) 在spring IoC容器仅存在一个Bean实例,Bean以单例方式存在,bean作用域范围的默认值。
request 每次HTTP请求都会创建一个新的Bean,该作用域仅适用于web的Spring WebApplicationContext环境。
prototype(原型) 每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时,相当于执行newXxxBean()。
session 同一个HTTP Session共享一个Bean,不同Session使用不同的Bean。该作用域仅适用于web的Spring WebApplicationContext环境。
application 限定一个Bean的作用域为ServletContext的生命周期。该作用域仅适用于web的Spring WebApplicationContext环境。

bean的生命周期

spring中的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注解标注在属性上就可以实现自动装配了

本文地址:https://blog.csdn.net/m0_46267375/article/details/109265317