spring学习笔记之---bean管理的注解方式
bean管理的注解方式
(一)使用注解定义bean
(1)常用注解

(2)实例
1.在pom.xml中进行配置
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
2.创建一个类,在类中写一个方法,在类上加一个注解@Component
package service;
import org.junit.Test;
import org.springframework.stereotype.Component;
import sun.misc.Contended;
@Component("UserService")
public class UserService {
public String Hello(){
return "hello";
}
}
3.创建一个applicationContext.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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描=======================-->
<context:component-scan base-package="service"></context:component-scan>
</beans>
4.创建一个log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, stdout
5.创建一个测试类
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
public class UserTest {
@Test
public void hellotest(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService=(UserService)applicationContext.getBean("UserService");
String s=userService.Hello();
System.out.println(s);
}
}
(二)属性注入的注解
(1)常用注解


1.属性注入
package service;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import sun.misc.Contended;
@Component("UserService")
public class UserService {
@Value("小欢")
private String name;
public String Hello(){
return "hello"+name;
}
}
2.类注入
(a)UserService.java
package service;
import dao.UserDao;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import sun.misc.Contended;
import javax.annotation.Resource;
@Component("UserService")
public class UserService {
@Value("小欢")
private String name;
@Resource(name="UserDao")
private UserDao userDao;
public String Hello(){
System.out.println("service中的hello");
return "hello"+name;
}
}
(b)UserDao.java
package dao;
import org.springframework.stereotype.Repository;
@Repository("UserDao")
public class UserDao {
public void Hello(){
System.out.println("dao 中的hello");
}
}
(c)applicationContext.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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描=======================-->
<context:component-scan base-package="service,dao"></context:component-scan>
</beans>
(d)UserTest.java
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
public class UserTest {
@Test
public void hellotest(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService=(UserService)applicationContext.getBean("UserService");
String s=userService.Hello();
System.out.println(s);
}
}
(三)其他注解


(四)xml和注解整合开发
1.UserService2.java
package service;
import dao.UserDao2;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
public class UserService2 {
@Resource(name="UserDao2")
private UserDao2 userDao2;
public void He(){
userDao2.He();
System.out.println("userservice2中的he");
}
}
2.UserDao2.java
package dao;
public class UserDao2 {
public void He(){
System.out.println("userdao2中的he");
}
}
3.applicationContext.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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="UserService2" class="service.UserService2"/>
<bean id="UserDao2" class="dao.UserDao2"/>
</beans>
4.UserTest.java
package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
import service.UserService2;
public class UserTest {
@Test
public void hetest(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService2 userService2=(UserService2)applicationContext.getBean("UserService2");
userService2.He();
}
}
spring学习笔记之---bean管理的注解方式的更多相关文章
- spring学习笔记之---bean管理
bean管理(xml) (一)spring的工厂类 FileSystemXmlApplicationContext 读取磁盘配置文件 (二)bean实例化的三种方式 (1)使用类构造器实例化(默认无参 ...
- (转)Spring的bean管理(注解方式)
http://blog.csdn.net/yerenyuan_pku/article/details/69663779 Spring的bean管理(注解方式) 注解:代码中的特殊标记,注解可以使用在类 ...
- Spring 的 Bean 管理(注解方式)
Spring 的 Bean 管理(注解方式) 1. 导入必要的 jar 包和 xml 文件 使用注解需要导入 spring-aop 的 jar 包. applicationContext.xml 文件 ...
- Spring学习笔记1——IOC: 尽量使用注解以及java代码(转)
在实战中学习Spring,本系列的最终目的是完成一个实现用户注册登录功能的项目. 预想的基本流程如下: 1.用户网站注册,填写用户名.密码.email.手机号信息,后台存入数据库后返回ok.(学习IO ...
- Spring学习笔记1——IOC: 尽量使用注解以及java代码
在实战中学习Spring,本系列的最终目的是完成一个实现用户注册登录功能的项目. 预想的基本流程如下: 1.用户网站注册,填写用户名.密码.email.手机号信息,后台存入数据库后返回ok.(学习IO ...
- Spring的bean管理(注解方式)
注解:代码中的特殊标记,注解可以使用在类.方法.属性上面,使用注解可实现一些基本的功能.注解的写法是@注解名称(属性=属性值). 使用注解创建对象 第一步,创建Web项目,引入Spring的开发包 第 ...
- Spring学习笔记(三)—— 使用注解配置spring
一.使用步骤 1.1 导包 1.2 为主配置文件引入新的命名空间(约束) 在applicationContext.xml中引入context约束 1.3 编写相关的类 public class Use ...
- (四)Spring 的 bean 管理(注解方式)
目录 前言 使用 aop 的配置文件写法 开启注解扫描 利用注解创建对象 注解方式注入属性 配置文件和注解混合使用 前言 注解可以写在 类.方法.属性 上 : 使用 注解,需要导入 aop 包: 使用 ...
- Spring学习笔记(3)——Bean的注入方式
依赖注入 依赖注入支持属性注入.构造函数注入.工厂注入. 属性注入: 属性注入即通过setXxx()方法注入Bean的属性值或依赖对象 属性注入要求Bean提供一个默认的构造函数(无参构造函数),并为 ...
随机推荐
- java常用基础(一)
Java常用基础(一) 原文写于2017-12-02 输入输出 //输入 Scanner in = new Scanner(new BufferedInputStream(System.in)); i ...
- 详解Linux运维工程师必备技能
张戈大神是腾讯的一名运维,张戈博客也是我接触到第一个 Linux 运维师的博客,最近也在接触 Linux,说到工具,在行外可以说是技能,在行内一般称为工具,就是运维必须要掌握的工具. 我就大概列出这几 ...
- 如何提高Sprint Review的质量?
Sprint Review不是回顾,其目标是演示这个Sprint中自己的工作成果,参会人员包括设计师.开发人员和Product Owner.在Worktile,我们尽量保持Sprint评审会的轻松随意 ...
- SQL参数化查询
参数化查询(Parameterized Query 或 Parameterized Statement)是指在设计与数据库链接并访问数据时,在需要填入数值或数据的地方,使用参数 (Parameter) ...
- Spring的<context:annotation-config>和<annotation-driven>
<context:annotation-config> 相对于注册 AutowiredAnnotationBeanPostProcessor.CommonAnnotationBeanPo ...
- 新手上路—Java的"瑞士军刀"
“ Jodd 是一个开源的 Java 工具集, 包含一些实用的工具类和小型框架.简单,却很强大!这在我们的日常开发工作中,无疑是如虎添翼,事半功倍. Jodd = Tools + IoC + MVC ...
- Skyline WEB端开发5——添加标签后移动
针对于标签或者模型,在skyline上可以进行移动.可以让一个模型可以像无人机似的飞行,或者描述从一个点到另一个点的飞行轨迹. 话不多说,直接上干货. 第一步 添加标签 参考网址:https://ww ...
- python实现DFA模拟程序(附java实现代码)
DFA(确定的有穷自动机) 一个确定的有穷自动机M是一个五元组: M=(K,∑,f,S,Z) K是一个有穷集,它的每个元素称为一个状态. ∑是一个有穷字母表,它的每一个元素称为一个输入符号,所以也陈∑ ...
- Tomcat CGIServlet enableCmdLineArguments远程代码执行_CVE-2019-0232漏洞复现
Tomcat CGIServlet enableCmdLineArguments远程代码执行_CVE-2019-0232漏洞复现 一.漏洞描述 该漏洞是由于tomcat CGI将命令行参数传递给Win ...
- 个人永久性免费-Excel催化剂功能第42波-任意字符指定长度随机函数
日常做表过程中,难免会有一些构造数据的场景,构造数据最好是用随机的数据,如随机密码,随机英文字母.数字等.在Excel原生的随机函数Rand中,仅能处理数字的随机,且最终生成的结果也是数字类型.今天E ...