吴裕雄--天生自然--SPRING--Spring IoC

发布时间:2022-07-05 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了吴裕雄--天生自然--SPRING--Spring IoC脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

1.声明Bean的注解

(1)@component
    该注解是一个泛化的概念,仅仅表示一个组件对象(Bean),可以作用在任何层次上,没有明确的角色。
   (2)@ReposITory
    该注解用于将数据访问层(DAO)的类标识为Bean,即注解数据访问层Bean,其功能与@Component()相同。
   (3)@Service
    该注解用于标注一个业务逻辑组件类(Service层),其功能与@Component()相同。
   (4)@Controller
    该注解用于标注一个控制器组件类(SPRing MVC的Controller),其功能与@Component()相同。
2.注入Bean的注解

   (1)@Autowired
    该注解可以对类成员变量、方法及构造方法进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除setter 和getter方法。默认按照Bean的类型进行装配。
   (2)@Resource
    该注解与@Autowired功能一样。区别在于,该注解默认是按照名称来装配注入的,只有当找不到与名称匹配的Bean才会按照类型来装配注入;而@Autowired默认按照Bean的类型进行装配,如果想按照名称来装配注入,则需要结合@Qualifier注解一起使用。
    @Resource注解有两个属性:name和tyPE。name属性指定Bean实例名称,即按照名称来装配注入;type属性指定Bean类型,即按照Bean的类型进行装配。
   (3)@Qualifier
    该注解与@Autowired注解配合使用。当@Autowired注解需要按照名称来装配注入,则需要结合该注解一起使用,Bean的实例名称由@Qualifier注解的参数指定。
1.使用Eclipse创建Web应用并导入JAR包
    2.创建DAO层
    3.创建Service层
    4.创建Controller层
    5.创建配置类
    6.创建测试类
    7.运行结果
package annotation.dao;

public interface testDao {
    public void save();

}
package annotation.dao;

import org.springframework.stereotype.Repository;

@Repository("TestDaoImpl")
public class TestDaoImpl implements TestDao{
    @override
    public void save() {
        System.out.println("testDao save");
    }

}
package annotation.service;

public interface TestService {
    public void save();

}
package annotation.service;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import annotation.dao.TestDao;

@Service("TestSeviceimpl")
public class TestSeviceImpl implements TestService{
    @Resource(name="TestDaoImpl")
    private TestDao testDao;
    @Override
    public void save() {
        testDao.save();
        System.out.println("testService save");
    }

}
package annotation.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import annotation.service.TestService;

@Controller
public class TestController {
    @Autowired
    private TestService testService;
    public void save() {
        testService.save();
        System.out.println("testController save");
    }

}
package annotation;

import org.springframework.context.annotation.COMponentScan;
import org.springframework.context.annotation.configuration;

@Configuration
@ComponentScan("annotation")
public class ConfigAnnotation {

}
package annotation;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import annotation.controller.TestController;


public class TestAnnotation {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext appCon = new AnnotationConfigApplicationContext(ConfigAnnotation.class);
        TestController tc = appCon.getBean(TestController.class);
        tc.save();
        appCon.close();
    }
}

吴裕雄--天生自然--SPRING--Spring IoC

 

脚本宝典总结

以上是脚本宝典为你收集整理的吴裕雄--天生自然--SPRING--Spring IoC全部内容,希望文章能够帮你解决吴裕雄--天生自然--SPRING--Spring IoC所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。