博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring学习笔记之---bean管理的注解方式
阅读量:4692 次
发布时间:2019-06-09

本文共 5303 字,大约阅读时间需要 17 分钟。

bean管理的注解方式

(一)使用注解定义bean

(1)常用注解

 

(2)实例

1.在pom.xml中进行配置

org.springframework
spring-jdbc
5.1.5.RELEASE
org.springframework
spring-context
5.1.5.RELEASE
org.springframework
spring-aop
5.1.5.RELEASE
org.springframework
spring-core
5.1.5.RELEASE
org.springframework
spring-beans
5.1.5.RELEASE
org.springframework
spring-tx
5.1.5.RELEASE
junit
junit
4.11

 

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,在里面配置包扫描

 

4.创建一个log4j.properties

### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.errlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### direct messages to file mylog.log ###log4j.appender.file=org.apache.log4j.FileAppenderlog4j.appender.file.File=c\:mylog.loglog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.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

 

(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

 

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();    }}

 

转载于:https://www.cnblogs.com/dyddzhp/p/11307755.html

你可能感兴趣的文章
filter 过滤器(监听)
查看>>
node启动时, listen EADDRINUSE 报错;
查看>>
杭电3466————DP之01背包(对状态转移方程的更新理解)
查看>>
kafka中的消费组
查看>>
python--注释
查看>>
SQL case when else
查看>>
SYS_CONTEXT 详细用法
查看>>
Pycharm配置autopep8让Python代码更符合pep8规范
查看>>
我的第一篇博客
查看>>
【C++算法与数据结构学习笔记------单链表实现多项式】
查看>>
C#垃圾回收机制
查看>>
31、任务三十一——表单联动
查看>>
python之hasattr、getattr和setattr函数
查看>>
maven使用阿里镜像配置文件
查看>>
Copy code from eclipse to word, save syntax.
查看>>
arguments.callee的作用及替换方案
查看>>
23 Java学习之RandomAccessFile
查看>>
P2709 小B的询问
查看>>
PHP echo 和 print 语句
查看>>
第一讲 一个简单的Qt程序分析
查看>>