导航:首页 > 文件管理 > spring注解的配置文件

spring注解的配置文件

发布时间:2024-07-05 05:45:55

1. spring的常用注解是什么

Spring Boot常用注解

1、@SpringBootApplication

替代 @SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan

2、@ImportAutoConfiguration

导入配置类,一般做测试的时候使用,正常优先使用@EnableAutoConfiguration

3、@SpringBootConfiguration

替代@Configuration

4、@ImportResource

将资源导入容器

5、@PropertySource

导入properties文件

6、PropertySources

@PropertySource 的集合

(1)spring注解的配置文件扩展阅读:

Controller 该类为Controller。

RequestMapping 配置方法路径等信息。

ResponseBody 返回值,例如JSON,XML。

PathVariable 获取RESTFUL路径中的值如 /company/{corpId}/dept/{deptId}。

RequestParam 获取Request参数值如xxx?from=index_nav。

Component,Repository,Service。

一般用Repository,service用Service,需要多个service时,一般用Componet。

2. spring怎么配置注解

@Repository注解:

1 package imooc_spring.test.anotation.myrepository;

2

3 import org.springframework.stereotype.Repository;

4

5 /**

6 * 指定id,默认为dAO,即类名首字母小写,如果指定了名称那么只能ctx.getBean(指定名称)来获取bean

7 * 这个例子里就只能通过ctx.getBean("wyl)来获取DAO 的实例了;

8 *

9 * @author Wei

10 */

11 @Repository("wyl")

12 public class DAO {

13 /**

14 * 返回x和y的乘积

15 *

16 * @param x

17 * @param y

18 * @return x*y

19 */

20 public int multi(int x, int y) {

21 return x * y;

22 }

23 }

复制代码

@Component 注解:

复制代码

1 package imooc_spring.test.anotation;

2

3 import org.springframework.stereotype.Component;

4 /**

5 * Component 注解

6 * @author Wei

7 *

8 */

9 @Component

10 public class TestObj {

11 public void SayHi(){

12 System.out.println(" Hi this is TestObj.SayHi()...");

13 }

14 }

复制代码

@Controller注解闷吵:

复制代码

1 package imooc_spring.test.anotation;

2

3 import org.springframework.stereotype.Controller;

4

5 @Controller

6 public class UserController {

7 public void execute(){

8 System.out.println(" UserController.execute()...");

9 }

10 }

复制代码

@Repository注解:

复制代码

1 package imooc_spring.test.anotation;

2

3 import org.springframework.stereotype.Repository;

4

5 //@Repository

6 @Repository("wyl_repo")

7 public class UserRepositoryImpl implements IUserRepository {

8 //模誉拍拟持久化层

9 庆罩羡 @Override

10 public void save() {

11 // TODO Auto-generated method stub

12 System.out.println(" UserRepositoryImpl.save()...");

13 }

14

15 }

复制代码

@Service注解:

复制代码

1 package imooc_spring.test.anotation;

2

3 import org.springframework.stereotype.Service;

4

5 @Service

6 public class UserService {

7 public void add(){

8 System.out.println(" UserService.add()...");

9 }

10 }

3. SpringBoot项目使用@PropertySource注解加载properties配置文件,但输出对象值为null

不是要加上前缀?
@ConfigurationProperties(prefix = "Example")
@PropertySource("classpath:my.properties")//这是属性文件路径

4. java开发中spring注解的使用方法

注解方式李态配置(简化XML配置文件)
组件自动扫描功能
首先需要在applicationContext.xml中添加<context:component-scan/>
a.扫描Bean组件的注解,替代xml中的<bean>元素的定义。
@Service 用于Service业务组件
@Control 用于Action控制组件
@Respository 用于DAO数据访问组哪源源件
@Component 用于其他组件
Bean组件扫描到容器后,裂羡默认名字为类名(首字母小写)
如果需要自定义名称可以使用@Service("id名")
b.依赖注入的注解标记
@Resource 按名称@Resource(name="id名")
@AutoWired 按名称
@Autowired
@Qualifier("id名")
c.其他注解
@Scope 等价于<bean scope="">
@PostConstruct 等价于<bean init-method="">
@PreDestroy 等价于<bean destroy-method="">

阅读全文

与spring注解的配置文件相关的资料

热点内容
什么网站信访最有效 浏览:396
魅蓝e2升级安卓70 浏览:438
黄石响应式网站建设多少钱 浏览:410
怎么把excel工作簿放到一个文件夹 浏览:949
wifi网络延时大怎么处理 浏览:345
云办公的原理是把传统文件放哪里 浏览:113
不属于群防群治队伍数据项有哪些 浏览:404
java树向上找 浏览:241
数据库查询票价 浏览:503
word黑色下划线怎么去掉 浏览:879
学习编程怎么学比较好 浏览:351
有什么好看的地图网站 浏览:593
oppo如何设置app黑名单 浏览:71
移动数据用了多少在哪里显示 浏览:549
excel表改变文件名颜色的方法 浏览:966
linuxshell二进制文件 浏览:36
什么是网络道德问题产生的 浏览:836
c清除文件夹 浏览:407
租房贷款用什么app 浏览:59
虚拟机oracle安装教程 浏览:745

友情链接