注解方式
注解种类
组件
Component
- @Component("normal"):指定bean id
- 如下为细分组件,没有语言层面的区别,仅仅为了区分所在分层
Service:服务层组件
Repository:持久层组件
Controller:控制器组件
自动装配Autowired
Qualifier:指定限定符
获取上下文方式
- 通过spring-test包的注解
@ContextConfiguration(classes = AppConfig.class)
主函数入口与代码解耦:新建配置类
- @Configuration:新建一个配置类,添加此注解
package com.verfallen.demo.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.verfallen.demo")
public class AppConfig {
}
- 扫描组件:需要写好包名,否则只去AppConfig所在包下寻找
测试方法
Junit + Spring-test
package com.verfallen.demo.controller;
import com.verfallen.demo.config.AppConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class ControllerTest {
@Autowired
private UserControl userControl;
@Test
public void test() {
userControl.add();
}
}
main方法:注解与xml
package com.verfallen.demo;
import com.verfallen.demo.controller.UserControl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class WebTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
// ApplicationContext context = new AnnotationConfigApplicationContext("com.verfallen.demo");
UserControl userControl = (UserControl) context.getBean("userControlNormal");
userControl.add();
}
}
xml方式
配置文件
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="daoNormal" class="demo.dao.impl.UserDaoNormal"></bean>
<bean id="daoFestival" class="demo.dao.impl.UserDaoFestival"></bean>
<bean id="service" class="demo.service.impl.UserServiceNormal">
<constructor-arg name="userDao" ref="daoFestival"/>
</bean>
<bean id="controller" class="demo.controller.impl.UserControlNormal">
<property name="userService" ref="service"></property>
</bean>
</beans>
获取上下文方式
package demo;
import demo.controller.UserControl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class WebTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationcontext.xml");
// ApplicationContext context = new AnnotationConfigApplicationContext("com.verfallen.demo");
UserControl userControl = (UserControl) context.getBean("controller");
userControl.add();
}
}
参考