zoukankan      html  css  js  c++  java
  • Spring的零配置

    @Component:标注一个普通的Spring Bean类。

    @Controller:标注控制器组件类。

    @Service:标注业务逻辑组件类。

    @Repository:标注DAO组件类。

    @Scope:指定Bean的作用域。

    @Resource:配置依赖。

    @PostConstruct和@PreDestroy:定制Bean生命周期的行为。

    @AutoWired:指定自动装配。

    @Qualifier:指定精确装配。

    bean.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:context="http://www.springframework.org/schema/context"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <!-- 自动搜索指定包及其子包下的所有Bean类 -->
        <context:component-scan base-package="com.lee">
        </context:component-scan>
    </beans>

    java类

    package com.lee.bean.impl;
    
    import javax.annotation.Resource;
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    
    import org.springframework.stereotype.Component;
    
    import com.lee.bean.Fruit;
    import com.lee.bean.Person;
    
    @Component("xiaoMing")
    public class XiaoMing implements Person {
    
        private Fruit fruit;
    
        public void eat() {
    
            fruit.description();
            System.out.println("yummy!");
        }
    
        public Fruit getFruit() {
            return fruit;
        }
    
        @Resource(name = "pear")
        public void setFruit(Fruit fruit) {
            this.fruit = fruit;
        }
        
        @PostConstruct
        public void init(){
            
            System.out.println("init...");
        }
        
        @PreDestroy
        public void destroy(){
            
            System.out.println("destroy...");
        }
    
    }

    精确装配

        @Autowired
        @Qualifier("pear")
        public void setFruit(Fruit fruit) {
            this.fruit = fruit;
        }
  • 相关阅读:
    OA日志模块
    OA查找用户
    在服务器注入脚本,然后客户端异步调用
    在使用ext2.0中使用store加载数据出现this.onMetaChange has no properties错误
    有关在div中的嵌套事件
    KML添加自定义数据
    KML 入门
    PostGIS中的shp2pgsql
    DOM基础
    jquery 操作 select,radio,checkbox等(转载)
  • 原文地址:https://www.cnblogs.com/harryV/p/3730063.html
Copyright © 2011-2022 走看看