Python 官方文档:入门教程 => 点击学习
目录使用注解开发1.bean2.属性如何注入3.衍生的注解4.自动装配5.作用域6.小结代码show1.新建一个模块:2.新建pojo包及类3.新建dao包及类4.新建service
在spring4之后,要使用注解开发,必须要保证aop的包导入了
使用注解需要导入context的约束,增加注解的支持
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解的支持-->
<context:annotation-config/>
</beans>
//等价于<bean id="user" class="com.Gongyi.pojo.User"/>
//@Component组件
@Component
public class User {
// 相当于<property name="name" value="gongyi"/>
@Value("gongyi")
public String name;
//@Value("muzi")
public void setName(String name) {
this.name = name;
}
}
@Component有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!
这四个注解功能都是一样的,都是代表将某个类注册到spring中,装配bean
- Autowired:自动装配通过类型。名字
如果Autowired不能唯一自动装配上属性,则需要通过 @Qualifier(value = "xxx")
- @Nullable 字段标记了这个注解,说明这个字段可以为null
- Resource:自动装配通过名字。类型
@Component
@Scope("singleton")
public class User {
// 相当于<property name="name" value="gongyi"/>
@Value("gongyi")
public String name;
//@Value("muzi")
public void setName(String name) {
this.name = name;
}
}
xml与注解:
xml与注解最佳实践:
<!--指定要扫描的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.gongyi"/>
<!--开启注解的支持-->
<context:annotation-config/>
代码结构图:
spring-06-anno
//等价于<bean id="user" class="com.gongyi.pojo.User"/>
//@Component组件
@Component
@Scope("singleton")
public class User {
// 相当于<property name="name" value="gongyi"/>
@Value("gongyi")
public String name;
//@Value("muzi")
public void setName(String name) {
this.name = name;
}
}
@Repository
public class UserDao {
}
@Service
public class UserService {
}
@Controller
public class UserController {
}
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--指定要扫描的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.gongyi"/>
<!--开启注解的支持-->
<context:annotation-config/>
</beans>
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user");
System.out.println(user.name);
}
}
1.被spring托管的类在idea中的显示
1)未被托管前
2)配置托管
3)托管后
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程网的更多内容!
--结束END--
本文标题: Java Spring的使用注解开发详解
本文链接: https://www.lsjlt.com/news/162409.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0