导航:首页 > 文件管理 > 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注解的配置文件相关的资料

热点内容
ai中线段怎么添加宽度配置文件 浏览:956
lol文件怎么找不到game 浏览:142
aecc视频教程 浏览:983
linux怎么查看数据库用户名 浏览:182
cefs文件系统 浏览:404
学平面设计个编程哪个好 浏览:701
如何把编程文件转为hex 浏览:80
清除苹果地图来自地址 浏览:233
已经打开的文件如何清理 浏览:685
视频网站有什么用 浏览:70
多个表格文件怎样压缩文件 浏览:729
cad文件大很卡如何解决 浏览:633
将java程序打包成apk 浏览:277
2021唱吧文件找不到了 浏览:463
华为p9手机文件管理 浏览:284
固定在工具栏的文件夹怎么查找路径 浏览:667
半条命2有几个版本 浏览:333
电脑管家微信备份文件夹 浏览:826
ubuntu文件夹解锁 浏览:34
网站多少钱一单 浏览:382

友情链接