@Component
  a) 初始化的名字默认为类名首字母小写:UserService 在容器中默认为 userService
  b) 可以指定初始化 bean 的名字:  @Component(value="userService")

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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:annotation-config /> <context:component-scan base-package="com.bjsxt" /><!-- 指定包 --> </beans>

bean 配置:

  UserDAOImpl

 package com.bjsxt.dao.impl;

 import org.springframework.stereotype.Component;

 import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User; @Component //默认名字:userDAOImpl
public class UserDAOImpl implements UserDAO{ public void save(User u) {
System.out.println("a user saved!");
} }

  UserService

 package com.bjsxt.service;

 import javax.annotation.Resource;

 import org.springframework.stereotype.Component;

 import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User; @Component(value="userService")
public class UserService { private UserDAO userDAO; public void init(){
System.out.println("init");
} public UserService(){} public UserService(UserDAO userDAO){
super();
this.userDAO = userDAO;
} public void addUser(User u){
this.userDAO.save(u);
} public UserDAO getUserDAO() {
return userDAO;
} @Resource(name="userDAOImpl")
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
} public void destroy(){
System.out.println("destroy");
} }

代码链接: http://pan.baidu.com/s/1i4GwEPn 密码: awdu

jar包链接: http://pan.baidu.com/s/1hs655SK 密码: 6h8x

@Component 注解的更多相关文章

  1. Spring中@Component注解,@Controller注解详解

    在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...

  2. Spring中@Component注解,@Controller注解详解

    在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...

  3. Spring boot之SpringApplicationBuilder,@@Configuration注解,@Component注解

    SpringApplicationBuilder: 该方法的作用是可以把项目打包成war包 需要配置启动类,pom.xml文件等,具体见:http://blog.csdn.net/linzhiqian ...

  4. controller,service,repository,component注解的使用对比

    项目中的controller层使用@controller注解 @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象.分发处理器将会扫描使用了 ...

  5. 分分享知识-快乐自己: @Component注解的使用

    @controller 控制器(注入服务) 用于标注控制层,相当于struts中的action层 @service 服务(注入dao) 用于标注服务层,主要用来进行业务的逻辑处理 @repositor ...

  6. Spring Boot@Component注解下的类无法@Autowired的问题

    title: Spring Boot@Component注解下的类无法@Autowired的问题 date: 2019-06-26 08:30:03 categories: Spring Boot t ...

  7. Spring Boot Service注入为null mapper注入为null @Component注解下@Value获取不到值 WebsocketServer类里无法注入service

    最近搞了一下websocket前台(这个网上有很多的教程这里就不班门弄斧啦) 以及前后台的交互 和后台的bug(搞了两天) 也是状态频发 bug不断 下面说一说问题. Websocket主类里面无法注 ...

  8. @Service ,@Controller,@Component注解

    首先,在applicationContext.xml文件中加一行: <context:component-scan base-package="com.hzhi.clas"/ ...

  9. SpringBoot1.X /2.X使用@Component注解注入为java.lang.NullPointException 问题小结

    问题简述: 我们在使用Spring系的产品的时候总是想当然的以为所有指定的Bean都会让Spring来管理,其实不然,即使是Spring出品的产品也不全是的哦,以下是我遇到的一个使用@Componen ...

  10. spring学习笔记三:Component注解(把POJO类实例化到spring的IOC容器中)

    Component注解:把普通的POJO 类实例化到spring的IOC容器中,就是定义成<bean id="" class=""> 项目目录树: ...

随机推荐

  1. 使用Jenkins进行Android自动打包,自定义版本号等信息【转】

    之前App在提交测试和最终部署的过程中App打包一直是由开发人员来完成的,由于项目比较大, 再加上Android打包本身就比较慢,所以每次打包还是很耗时的.并且按照严格的研发流程来讲,开发人员应该只负 ...

  2. CentOS 7 设置日期和时间

    现代操作系统分为以下两种类型的时钟: 实时时钟(Real-Time Clock,RTC),通常称为硬件时钟(一般是系统主板上的集成电路),它完全独立于操作系统的当前状态,即使在计算机关闭时也能运行. ...

  3. C++_代码重用4-多重继承

    继承使用时要注意,默认是私有派生.所以要公有派生时必须记得加关键字Public. MI(Multi Inheritance)会带来哪些问题?以及如何解决它们? 两个主要问题: 从两个不同的基类继承同名 ...

  4. [转] Spring Boot实战之Filter实现使用JWT进行接口认证

    [From] http://blog.csdn.net/sun_t89/article/details/51923017 Spring Boot实战之Filter实现使用JWT进行接口认证 jwt(j ...

  5. cotex_A7/A9:

    A7与A9的比较:A7计算性能(DMIPS)不如A9(编号越大计算能力越强),但A7功耗更先进,作为A15的协处理器.A8是单核.

  6. white-space中 pre pre-line pre-wrap的区别

    css3为white-space新增了2个非常高冷的值:pre-line与pre-wrap. 现在我们来看一下它们与pre之间的区别呢? pre:会保留空格,但我想它与pre-line/pre-wra ...

  7. vue-scroller的使用 && 开发自己的 scroll 插件

    vue-scroller的使用 在spa开发过程中,难免会遇到使用scroll的情况,比如下面的: 即,当用户选择好商品之后,点击购物车,就会有一个购物车弹窗,如果选择的商品小于三个,刚好合适,如果多 ...

  8. 微博关系服务与Redis的故事

    http://www.infoq.com/cn/articles/weibo-relation-service-with-redis?utm_source=articles_about_Redis&a ...

  9. lua实现List及Dictionary

    转载:http://www.maosongliang.com/archives/122 参考 http://blog.csdn.net/jason_520/article/details/541736 ...

  10. CF 305B——Continued Fractions——————【数学技巧】

    B. Continued Fractions time limit per test 2 seconds memory limit per test 256 megabytes input stand ...