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. Tornado demo3 - tcpecho分析

    在这个demo中,主要是使用了Tornado中异步的TCP client和server来实现一个简单的echo效果(即客户端发送的message会从server端返回到client).代码的githu ...

  2. Odoo Documentation : Environment

    Environment The Environment stores various contextual data(上下文数据 ) used by the ORM: the database cur ...

  3. idea打包可执行jar

    (1)在项目上鼠标右键 --> Open Module Settings 或者点击工具栏上的 (2)Artifacts --> + --> JAR --> From modul ...

  4. spring boot项目搭建中遇到的问题

    自己动手搭建一下spring boot的项目,中途遇到了几个问题,在这里记录一下! 一.关于数据库中的表设计的问题 1.设计表的时候一定要添加的两个字段created updated 创建时间与更新时 ...

  5. csp-s模拟48,49 Tourist Attractions,养花,画作题解

    题面:https://www.cnblogs.com/Juve/articles/11569010.html Tourist Attractions: 暴力当然是dfs四层 优化一下,固定两个点,答案 ...

  6. https://blog.csdn.net/u012235003/article/details/54576737

    https://blog.csdn.net/u012235003/article/details/54576737

  7. python3 新特性

    1.格式化字符串f-string user = "Mike" log_message = f'User{user} has logged in' 2.路径管理库Pathlib 3. ...

  8. JQuery--漂亮的三目运算与jQ选择器结合代码

    $(function($) { $("input[name='timeset']").bind('click', function() { $(this).val() == 'cu ...

  9. [转载] OpenCV2.4.3 CheatSheet学习(一)

    OpenCV向MATLAB靠拢,图像的操作方法变得不那么C了,更m了一些.比如,MATLAB中的常用函数imshow.imread.imwrite函数在OpenCV中已经有了同名的兄弟. 此外,Ope ...

  10. NDK(23) 使用CMake 构建 c/c++代码库

    1.官网 https://developer.android.com/studio/projects/add-native-code.html 2.android studio 安装相关工具 在打开的 ...