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提供一个默认的构造函数(无参构造函数),并为 ...
随机推荐
- Django框架rest_framework中APIView的as_view()源码解析、认证、权限、频率控制
在上篇我们对Django原生View源码进行了局部解析:https://www.cnblogs.com/dongxixi/p/11130976.html 在前后端分离项目中前面我们也提到了各种认证需要 ...
- Python笔记【4】_字典学习
#!/usr/bin/env/python #-*-coding:utf-8-*- #Author:LingChongShi #查看源码Ctrl+左键 ''' dict:字典以“{}”包围,以“键:值 ...
- There is no getter for property named 'username' in 'class Model1.User'-----报错解决
There is no getter for property named 'username' in 'class Model1.User' -----Model Model1.User'中没有名为 ...
- 整理一下Apache与Tomcat的关系
如果有对Apache与Tomcat认识比较的同学就要拿起小板凳听一下 关系一 Apache是web服务器,Tomcat是应用服务器,也就是java服务器,因为Apache只能支持静态网页,所以Apac ...
- 我以为我对Mysql索引很了解,直到我遇到了阿里的面试官
GitHub 4.8k Star 的Java工程师成神之路 ,不来了解一下吗? GitHub 4.8k Star 的Java工程师成神之路 ,真的不来了解一下吗? GitHub 4.8k Star 的 ...
- Thinkphp5.0之异常处理
1.默认异常处理在调试模式下,系统默认展示的错误页面:请输入图片描述 异常处理接管 1.修改config.php 'app_debug' => false,2.在配置文件里添加如下代码 // 异 ...
- Codeforces Gym100962J:Jimi Hendrix(树型DP)
http://codeforces.com/gym/100962/attachments 题意:有一个n个节点的字母树,给出n-1条边的信息,代表边上有一个字母,然后给出长度为m的字符串,问是否能在这 ...
- SqlCommand的Parameters的用法
SqlCommand的Parameters的用法 可以用的SqlCommand的Parameters的方法SqlCommand cmd=new ("insert into notice(ly ...
- 关于pcl索引的使用
最近开始动手做实验,之前写了一个小实验利用到了PCL库中的索引: 现在在写利用PCL中的RegionGrowing类来分割生成面片,无论是迭代生成还是进行提取都需要用到pcl库中定义的索引, 虽然搞的 ...
- DAX 第二篇:计算上下文
计算上下文是计算公式的环境,任何DAX表达式都是在上下文中求值的.行上下文和筛选上下文是DAX中仅有的上下文类型,把这两种上下文称为计算上下文.计算上下文用于限定公式计算的环境,当上下文变化时,相同的 ...