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

热点内容
造假app有哪些 浏览:970
不知道宽带账号密码怎么连接网络 浏览:721
卸载喜马拉雅时显示找不到文件 浏览:858
蜂窝热点的数据漫游是什么 浏览:392
乐高建模编程什么关系 浏览:9
win10怎么创建镜像文件 浏览:324
手机文件转码 浏览:717
jsp首页获取菜单 浏览:796
js获取数组里面的值吗 浏览:422
东莞哪里有少儿编程课 浏览:782
有的视频文件无法播放 浏览:189
如何关闭同花顺左边的app 浏览:685
上海那个酒店专线网络 浏览:508
macae安装目录在哪个文件夹 浏览:664
微信文件在哪里iphone 浏览:664
医学用书哪个网站 浏览:795
联通拉光纤数据多少网速最好 浏览:502
java删除文档中的字符串 浏览:733
wireshark获取qq密码 浏览:767
java程序设计简答题题答案 浏览:142

友情链接