【spring】spring学习笔记

spring对bean的管理(基于XML)

bean的实例化

方式一 使用默认构造函数

<bean id="userService" class="com.itheima.serive.impl.UserServiceImpl"></bean>

方式二 使用工厂中的方法

public class InstanceFactory {
    public IUserService getUserService(){
        return new UserServiceImpl();
    }
}
<bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
<bean id="userService" factory-bean="instanceFactory" factory-method="getUserService"></bean>

方式三 使用工厂中的静态方法

public class StaticFactory {
    public static IUserService getUserService(){
        return new UserServiceImpl();
    }
}
<bean id="userService" class="com.itheima.factory.StaticFactory" factory-method="getUserService"></bean>

bean的作用域

bean标签的scope属性
	singleton:单例的(默认值)
	prototype:多例的
	request:作用于web应用的请求范围
	session:作用于web应用的会话范围
	global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session

bean的生命周期

单例对象
		出生:当容器创建时对象出生
		活着:只要容器还在,对象一直活着
		死亡:容器销毁,对象消亡
		总结:单例对象的生命周期和容器相同
多例对象
		出生:当我们使用对象时spring框架为我们创建
		活着:对象只要是在使用过程中就一直活着。
		死亡:当对象长时间不用,且没有别的对象引用时,由Java的垃圾回收器回收

bean的依赖注入

构造函数注入

public class User {
    private Integer id;
    private String username;
    private Date birthday;

    public User(Integer id, String username, Date birthday) {
        this.id = id;
        this.username = username;
        this.birthday = birthday;
    }

    public Integer getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public Date getBirthday() {
        return birthday;
    }
}
<bean id="user" class="com.itheima.pojo.User">
    <constructor-arg name="id" value="1"></constructor-arg>
    <constructor-arg name="username" value="jack"></constructor-arg>
    <constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<bean id="now" class="java.util.Date"></bean>

set方法注入

public class User {
    private Integer id;
    private String username;
    private Date birthday;
    
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}
    <bean id="user" class="com.itheima.pojo.User">
        <property name="id" value="1"></property>
        <property name="username" value="jack"></property>
        <property name="birthday" ref="now"></property>
     </bean>
    <bean id="now" class="java.util.Date"></bean>

注入集合数据

package com.itheima.utils;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class CollectionInjection {
    private List<String> list;
    private Set<String> set;
    private Map<String,String> map;
    private Properties props;

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

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

    public Set<String> getSet() {
        return set;
    }

    public void setSet(Set<String> set) {
        this.set = set;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

    public Properties getProps() {
        return props;
    }

    public void setProps(Properties props) {
        this.props = props;
    }
}
<bean id="collectionInjection" class="com.itheima.utils.CollectionInjection">
     <!--给List注入数据(基本类型)-->
     <property name="list">
         <list>
             <value>1</value>
             <value>2</value>
             <value>3</value>
         </list>
     </property>
     <!--给List注入数据(引用类型)-->
     <property name="list">
         <list>
             <ref bean="user1"></ref>
             <ref bean="user2"></ref>
             <ref bean="user3"></ref>
         </list>
     </property>

     <!--给Set注入数据(基本类型)-->
     <property name="set">
         <set>
             <value>a</value>
             <value>b</value>
             <value>c</value>
         </set>
     </property>
     <!--给Set注入数据(引用类型)-->
     <property name="set">
         <set>
             <ref bean="user1"></ref>
             <ref bean="user2"></ref>
             <ref bean="user3"></ref>
         </set>
     </property>

     <!--给Map注入数据(基本类型)-->
     <property name="map">
         <map>
             <entry key="red" value=""></entry>
             <entry key="green" value="绿"></entry>
             <entry key="blue"><value></value></entry>
         </map>
     </property>
     <!--给Map注入数据(引用类型)-->
     <property name="map">
         <map>
             <entry key="u1" value-ref="user1"></entry>
             <entry key="u2" value-ref="user2"></entry>
             <entry key="u3" value-ref="user3"></entry>
         </map>
     </property>

     <!--给Properties注入数据-->
     <property name="props">
         <props>
             <prop key="small"></prop>
             <prop key="middle"></prop>
             <prop key="big"></prop>
         </props>
     </property>
     
 </bean>

spring对bean的管理(基于注解)

扫描指定包下的所有类

<context:component-scan base-package="类所属的包名"></context:component-scan>

bean的实例化

持久层

@Repository("userDao")
public class UserDaoImpl implements IUserDao {
	......
}

业务层

@Service("userService")
public class UserServiceImpl implements IUserService {
	......
}

控制层

@Controller
public class UserController {
    ......
}

其他

@Component("txManager")
public class TransactionManager {
	......
}
  • @Repository @Service @Controller 的本质都是@Component注解
  • 这些注解的作用:用于把当前类对象存入spring容器中。
  • 属性:value用于指定bean的id。当我们不写时,它的默认值是当前类名,且首字母改小写。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值