Spring(二)

bean的作用范围:

  bean标签中有scope属性来指定:

  singleton(siNGɡəlt(ə)n):单例模式   只创建一次  一致存在下去  

  prototype(ˈprōdəˌtīp):多例模式 每次使用都会重新创建

bean生命周期中的两个方法(bean标签属性):

  init-method: 执行构造后要进行的动作

  destory-method:销毁前要进行的动作

bean的生命周期是由容器来进行管理  容器关闭将销毁bean ,不关闭前

需要手动进行销毁,ApplicationContext的子类ClassPathXmlApplicationsContext

具有close方法。

销毁的方法只能作用于单例模式   多例模式 由垃圾回收机制管理。

Spring的依赖注入:在ioc容器运行过程中动态将依赖关系注入到对象中

  1.通过构造方法注入:通过<construct-arg   ......   />   在bean内容中书写

    标签中的属性: name:名称  type:数据类型 index:索引  ref:赋值其他的bean类型  配置过的bean  value:值

    <bean name="date" class="java.util.Date"></bean>
    <bean id="student" class="com.offcn.entity.Student" >
        <constructor-arg name="id" value="1"></constructor-arg>
        <constructor-arg name="name" value="张三"></constructor-arg>
        <constructor-arg name="birthday" ref="date"></constructor-arg>
    </bean>

  2.通过setter注入:<property  name=""   ref= "">  书写在bean内容中

<bean id="student" class="com.offcn.entity.Student">
    <property name="id" value="2"></property>
    <property name="name" value="李四"></property>
</bean>

   3.通过p标签:实质还是setter方法注入   换头部 在bean标签的中:p:name=""

<beans xmlns="http://www.springframework.org/schema/beans"201
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="student" class="com.offcn.entity.Student" p:id="1" p:name="李四"/>

配置文件模块化:将一个配置文件 分为多个配置文件

  创建applicationContext时可以传入多个xml名,

  在主配置文件中导入其他的配置文件

注:

  在同一个xml文件中id不能重复   主配置文件与其他配置文件的bean的id

可以相同 ,但是后加载的bean会覆盖前面的bean

spring注入数组  集合 properites类型数据

<bean id="commutils" class="com.offcn.entity.CommUtils">
<property name="array" >
    <array>
          <value>111111</value>
          <value>111111</value>      
    </array>               
</property>

<property name="list" >
    <list>
          <value>111111</value>
          <value>111111</value>      
    </list>               
</property>
    <property name="array" >
    <array>
          <entry key ="1111" value="11111"></value>
          <entry key = "111" value ="1111"></value>      
    </array>               
</property>
<property name="properites" >
    <props>
          <prop name = "11">111111</value>
          <prop name="222" >111111</value>      
    </props>               
</property>

Spring与jdbc的整合:

Spring提供了ioc容器,管理jdbc操作数据库过程中需要的的数据库连接对象,同时spring提供了整合jdbc操作数据

库的工具类JdbcDaoSupport和模板工具JdbcTemplate,JdbcTemplate提供了大量操作数据库的方法,

配置文件

注入的对象 必须是spring容器进行管理的对象

得到数据源

 

得到jdbcTemplate  注入 数据源的对象

 

得到userDao   注入 jdbcTemplate  

 

得到UserService对象  注入userDao对象

 

JdbcTemplateapi

  1. query()查询所以的数据

 

  1. queryForObject() 查询单条记录

 

Spring注解配置IOC

  1.导入依赖 

   2.编写配置文件:支持注解的头部

<?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">
    <context:annotation-config/>
</beans>

  3.编写注解

@Component("userDao")
public class UserDaoImpl implements UserDao {
    public int add() {
        System.out.println("1111111111");
        return 0;
    }
}

  4.在xml文件中扫描注解

 <context:component-scan base-package="com.offcn"></context:component-scan>

  5.测试

public class Test {
    @org.junit.Test
    public void show(){
        ApplicationContext app = new ClassPathXmlApplicationContext("bean.xml");
        UserDao userDao = (UserDao) app.getBean("userDao");
        userDao.add();
    }
}

@Component:实例化当前对象  并使用spring来进行管理   value属性与id作用相同 没有的话就是当前类的名称首字母小写

@Repository:编写dao层的注解

@Service:编写service层的注解

@Controller:编写表现层的注解

默认都是使用无参构造

@Autowired:在类中注入需要使用的对象  一般写在方法或变量上   多在变量上

  规则:根据类型进行注入 ,有且只能匹配一个,匹配多个或0个都会报错

@qualified:按照名称进行匹配    与上者结合使用  不能单独使用  value进行设置匹配的名称

java给spring提供的一个注解:

@Resource:jdk1.6之后    name 进行设置    作用上面两个的结合   先类型匹配在名称 

设置单例

@Scope("singleton")

生命周期的方法注解:

@Predestory:对象销毁前的动作

@PostConstruct:调用构造后的动作

<?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">

    <context:annotation-config/>

</beans>

Spring中的注解:
实例化对象注解:
@Component:用于实例化对象 具有value属性 写在类的上方 当类不归属三层架构时使用
@Service:service层对象
@controller:
@Repository:持久层对象
依赖注入注解:
@AutoWired:按照类型进行注入 匹配0个或多个都会报错 当时用该注解来注入属性时 set方法可以省略
@Qualifier:在上者的基础上在按照名称进行匹配 具有 value属性
@Value:用于简单类型的注入
改变注入范围的注解
@Scope 
其他:
@Configuration:指定类为配置类取代xml文件 写在类上
@Bean :写在方法上 表示该方法创建一个对象 并放在Spring容器中,具有value属性
@ComponentScan:执行spring初始化容器需要扫描的包 写在类上
@PropertySource:加载properites文件 写在类上

与junit整合
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:.xml文件或配置类.class")
原文地址:https://www.cnblogs.com/frhl/p/13590796.html