虽然不简单但还是要学的JavaWeb—Spring_IOC
2017-10-15 14:29
495 查看
初学,如果有错误,欢迎大家指正。
IOC :控制反转,spring框架的核心之一
即,将对象的创建权交给spring来管理。
1.导入spring的核心包
|___Beans
|___Core
|___Context
|___Expression
2.配置applicationContext.xml
配置好beans标签
id属性为bean的名称信息
class属性为实体类的路径。
获取bean:
方法一(手动获取):
方式二(自动扫描+注解)
IOC :控制反转,spring框架的核心之一
即,将对象的创建权交给spring来管理。
1.导入spring的核心包
|___Beans
|___Core
|___Context
|___Expression
2.配置applicationContext.xml
配置好beans标签
<bean id="" class=""></bean>
id属性为bean的名称信息
class属性为实体类的路径。
获取bean:
方法一(手动获取):
ApplicationContext ac = new ClassPathXmlApplicationContext(application.xml); Object object = ac.getBean("配置文件中bean的id"); object.function();
方式二(自动扫描+注解)
applicationContext.xml中只需要配置自动扫描 <context:component-scan base-package="要扫描的路径"></context:component-scan> 在bean的类名上方添加注解@Component(value="该bean的id") 此处的value属性代表bean的id。 首先在需要使用该bean的类的类名上方添加注解@ContentConfiguration("applicationContext.xml") 将配置文件引入。然后在类的声明上添加注解@AutoWired(默认按类型装配) 或者@AutoWired @Qualifier(value="")(强制使用名称)与Resource(name="bean的ID")相同。 eg: bean @Component(value="Person") class Person{ private String name; private int age; public String getName(){ return name; } public void setName(String name){ this.name = name; } } service 实现类 @Component(value="userServiceImpl") public void UserServiceImpl implements UserService{ @AutoWired @Qualifier(value="Person") private Person person; public void showName(){ person.setName("小王"); System.out.println(person.getName); } } test 测试类 @ContentConfiguration("applicationContext.xml") public class Test{ @Resource(name="userServiceImpl") private UserServiceImpl userImpl; public static void main(String args[ 9c40 ]){ userImpl.showName(); } }
相关文章推荐
- 虽然不简单但还是要学的JavaWeb—Spring_DI
- 虽然不简单但还是要学的JavaWeb—spring框架简介
- 虽然不简单但还是要学的JavaWeb—MyBatis
- 虽然不简单但还是要学的JavaWeb—配置环境
- 虽然不简单但还是要学的JavaWeb—SpringMVC简介
- 【JavaWeb-23】spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- Spring+struts2+hibernate框架的Java Web项目利用POI导出Excel简单例子
- HzhJava框架搭建二:添加tomcat和spring运行一个简单web程序
- Java - 如何在Web项目中配置Spring的IoC容器?
- 已经加载了类库,还是报错启动报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderList
- 使用DOM和java反射实现简单的SpringIOC
- Java - 在Web项目中如何获得Spring的IoC容器?
- Java WEB SpringMvc框架简单入门
- JAVA 中web 框架Spring 的核心 aop 和ioc
- 虽然不简单但还是要学的JavaWeb—spring事务管理
- 已经加载了类库,还是报错启动报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderList
- Spring IOC 方式结合TESTGN测试用例,测试简单java的命令模式
- JAVAWEB开发之Spring详解之——Spring的入门以及IOC容器装配Bean(xml和注解的方式)、Spring整合web开发、整合Junit4测试
- 用java的spring实现一个简单的IOC容器示例代码
- 用Java 开发 WebService Axis简单实例