Struts 2和hibernate也使用注解,但是使用注解在以后的开发中应用不多。但是可以说在整合的时候如何进行注解开发。在Spring中,注解必须会玩。

package cn.itcast.spring3.demo1;

import org.springframework.stereotype.Component;

/**
* 注解的方式装配Bean.
* @author zhongzh
*
*/
//在Spring配置文件中<bean id="userService" class="cn.itcast.demo1.UserService">
//其实也需要核心配置文件,只不过不是用来装配bean了,你得告诉Spring需要去哪些包下面去扫描.它得扫描到这些注解才行.它得去哪里扫描你得告诉人家才行
//@Component(value="userService")
@Component("userService")
public class UserService { public void sayHello(){ System.out.println("Hello Spring Annotation.....");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- 基于注解的开发需要引入context命名空间。它就是用来配置去扫描哪些包的. -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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></context:annotation-config><!-- XML和注解混搭的时候用的 -->
<!-- 告诉Spring你要扫描哪些包下的东西 -->
<!--
<context:component-scan base-package="cn.itcast.spring3.demo1"> -->
<context:component-scan base-package="cn.itcast.spring3.demo1"><!-- 纯注解用扫描的就行 --> </context:component-scan> </beans>
package cn.itcast.spring3.demo1;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; //注解的方式:都是将类装载到容器中.
public class SpringTest1 {
@Test
public void demo1(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) applicationContext.getBean("userService");
userService.sayHello();
} }

day38 16-Spring的Bean的装配:注解的方式的更多相关文章

  1. 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比

    [原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...

  2. (转)Spring的bean管理(注解方式)

    http://blog.csdn.net/yerenyuan_pku/article/details/69663779 Spring的bean管理(注解方式) 注解:代码中的特殊标记,注解可以使用在类 ...

  3. Spring ( 三 ) Spring的Bean的装配与生命周期、专用测试

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.对象的生命周期 1.IOC之Bean的生命周期 创建带有生命周期方法的bean public cla ...

  4. [spring]spring的bean自动装配机制

    7.bean的自动装配 是spring满足bean依赖的一种方式 spring会在上下文中自动寻找,并自动给bean装配属性 spring的装配方式: (1)手动装配 在people类中依赖了cat和 ...

  5. Spring 的 Bean 管理(注解方式)

    Spring 的 Bean 管理(注解方式) 1. 导入必要的 jar 包和 xml 文件 使用注解需要导入 spring-aop 的 jar 包. applicationContext.xml 文件 ...

  6. Spring温故而知新 - bean的装配(续)

    按条件装配bean 就是当满足特定的条件时Spring容器才创建Bean,Spring中通过@Conditional注解来实现条件化配置bean package com.sl.ioc; import ...

  7. Spring温故而知新 - bean的装配

    Spring装配机制 Spring提供了三种主要的装配机制: 1:通过XML进行显示配置 2:通过Java代码显示配置 3:自动化装配 自动化装配 Spring中IOC容器分两个步骤来完成自动化装配: ...

  8. Spring的bean管理(注解方式)

    注解:代码中的特殊标记,注解可以使用在类.方法.属性上面,使用注解可实现一些基本的功能.注解的写法是@注解名称(属性=属性值). 使用注解创建对象 第一步,创建Web项目,引入Spring的开发包 第 ...

  9. Spring 中Bean的装配方式

    最近又买了一本介绍SSM框架的书,是由黑马程序员编写的,书上讲的很好理解,边看边总结一下.主要总结一下bean的装配方式. Bean的装配可以理解为依赖系统注入,Bean的装配方式即Bean依赖注入的 ...

  10. Spring温故而知新 – bean的装配

    Spring装配机制 Spring提供了三种主要的装配机制: 1:通过XML进行显示配置 2:通过Java代码显示配置 3:自动化装配 自动化装配 Spring中IOC容器分两个步骤来完成自动化装配: ...

随机推荐

  1. 利用jQuery获取jsonp

    前端js代码: $.ajax({ url: 'http://localhost:8080/webApp/somejsonp', dataType: "jsonp", jsonp: ...

  2. Luogu P3558 [POI2013]BAJ-Bytecomputer(线性dp)

    P3558 [POI2013]BAJ-Bytecomputer 题意 给一个只包含\(-1,0,1\)的数列,每次操作可以让a[i]+=a[i-1],求最少操作次数使得序列单调不降.若无解则输出BRA ...

  3. Windows API 第21篇 DeleteVolumeMountPoint 删除挂载点

    函数原型:BOOL DeleteVolumeMountPoint(                                                      LPCTSTR lpszV ...

  4. leetcode 75 Sorted Colors

    两种解法 1)记录0和1的个数 然后按照记录的个数将0和1重新放入原数组,剩下的补2 2)双指针left,right left表示0~left-1都为0,即i之前都为0 right表示right+1~ ...

  5. NOI2018 Day1 归程(return)

    第一次参加NOI,当然,我没去现场做,只是在网络同步赛做了而已. 那网站,特别特别卡啊-- 最后只交了第一题,原本认为能AC,但是因为某些原因只有50分. 我这可怜的第一次啊-- 题目 题目点此处下载 ...

  6. 如何设置td中溢出内容的隐藏显示

    <style type="text/css"> table { table-layout:fixed; } td { overflow:hidden; word-bre ...

  7. Spring.之.报错:Caused by: java.lang.IllegalArgumentException: No Spring Session store is configured: set the 'spring.session.store-type' property

    Spring.之.报错 No Spring Session store is configured springboot在启动的时候报如下错误: Error starting ApplicationC ...

  8. 64位电脑上安装MySQL进行MFC开发的相关问题

    本人环境: 64位win7 + MySQL5.6 安装MySQL的时候有个选项是 选择:32位/64位.默认情况下是安装当前操作系统的位数.但我在使用VS进行开发的时候发现问题: error LNK2 ...

  9. 安装mysql-workbench

    sudo apt-get install mysql-workbench

  10. Visual studio 编译错误 无法将文件obj\Debug\*.exe复制到bin\Debug\*.exe

    问题: 错误 未能将“obj\Debug\Server.exe”复制到“bin\Debug\Server.exe”.超出了重试计数 10.失败. Server 错误 无法将文件“obj\Debug\S ...