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;
        }
  • 相关阅读:
    Bootstrap_警示框
    Bootstrap_标签
    Bootstrap_分页
    Bootstrap_导航条
    Bootstrap_导航
    Bootstrap_按钮工具栏
    Bootstrap_下拉菜单
    Bootstrap_网格系统
    Bootstrap_表单_图标
    统计学习方法 李航---第12章 统计学习方法总结
  • 原文地址:https://www.cnblogs.com/harryV/p/3730063.html
Copyright © 2011-2022 走看看