1. 导入jar包 java
HelloWorld中的必备包便可(与注解相关的jar包是context包) mysql
2. 引入Spring的配置文件 web
注意:用注解开发时,必定要导入context的约束文件 spring
导入约束文件,在配置文件中添加约束的步骤,请参看4.2-4.3 sql
可是必定要注意: express
在配置文件中context的添加约束文件时,已经有的就不要再添加了,只须要添加新引入的 session
context约束,并且, prefix不能够为空,自行填写,可是建议填context app
添加完成后: dom
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ide
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd ">
</beans>
3. 编写实体类,并在实体类中使用注解开发
@ Component (value="annotation")
//至关于配置文件中的<bean name=” annotation”class=”xxx.xx. MyAnnotation”></bean>
public class MyAnnotation {
@Value(value = "xiaoxiao")
//至关于配置文件中的<property name=”name” value=”xiaoxiao”></property>
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "MyAnnotation [name=" + name + "]";
}
}
4. 在配置文件中,添加要扫描注解的包(重点,必不可少!!!)
<context:component-scan base-package="cn.xiaoge.domain"></context:component-scan>
5. 编写测试类
public void fun1(){
//因为配置文件放在了包下面,在加载配置文件时,要填写完整路径
ClassPathXmlApplicationContext ac =
new ClassPathXmlApplicationContext("cn/xiaoge/annotation/annotation.xml");
MyAnnotation myann = (MyAnnotation) ac.getBean("annotation");
System.out.println(myann);
}
@Component:组件: 至关于在配置文件中注册bean
Spring提供了@Component的注解的一些衍生注解:(更易理解)
* @Controller : 标识web层的javabean
* @Service: 标识为service层的javabean
* @Repository : 标识为dao层的javabean
@Value:注入普通类型的属性
@Autowired (主要针对引用类型的数据封装)
默认按类型完成属性的注入, 若是存在两个同样的引用类型,则会报错:
* 所以,习惯采用按名称注入,
* 强制使用按名称的方式完成属性的注入:
* @Qulifer(value=”名称”)
@Resource 至关于 @Autowired + @Qulifer
@PostConstruct: 添加该注解的方法为初始化方法, 至关于init-method
@PreDestory: 添加该注解的方法为销毁方法, 至关于destroy-method
@Scope: 注解用来修饰bean的做用域
至关于scope属性:
* singleton 单例,只容许生成一个对象,用户访问的也是同一个
* prototype 多例,每次访问都会生成一个新的对象
* request 一次请求
* session 一次会话
以JavaConfig为核心:使用Java类做为配置文件.
* 类的构造特别麻烦!!!
@Configuration
public class BeanConfig {
@Bean(name="car")
public Car showCar(){
Car car = new Car();
car.setName("马自达");
car.setPrice(150000d);
return car;
}
@Bean(name="product")
public Product showProduct(){
Product product = new Product();
product.setName("空调");
product.setPrice(1200d);
return product;
}
}
XML:结构清晰.(Bean管理由Spring控制.)
注解:开发便捷.(属性注入:不须要提供set方法.)
企业中一般还有一种开发方式:XML和注解的整合开发.
* XML用于管理Bean.
* 注解用于属性注入.
须要在配置文件中开启注解配置:
<context:annotation-config/>
把Bean交给Spring进行管理.属性注入由注解完成.
核心jar包
context beans core expression web
logging lo4j
其中:web包是为了保证配置文件只加载一次
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置文件的存放位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(xxx);
//在servlet中, xxx是getServletContext()
//在action中,xxx能够是ServletActionContext().getServletContext()
Car car = (Car) ac.getBean("car");
System.out.println(car);
web所需包 + test包 + 依赖包中的junit包
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class TestJunit {
@Resource(name="user2")
private User user;
@Test
public void fun1(){
System.out.println(user);
}
}
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
mysql-connector-java-5.0.8-bin.jar
<!-- 配置properties文件-->
<context:property-placeholder location="classpath:cn/xiaoge/l_jdbcTemplate/db.properties"/>
<bean name="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}" ></property>
<property name="jdbcUrl" value="${url}" ></property>
<property name="user" value="${user}" ></property>
<property name="password" value="${password}" ></property>
<property name="maxPoolSize" value="5" ></property>
</bean>
<!-- 配置jdbc模板,并且必定要注意:dataSource要大写,大写!!!! -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<property name="dataSource" ref="datasource" ></property>
</bean>
<!-- 配置UserDao -->
<bean name="userDao" class="cn.xiaoge.l_jdbcTemplate.UserDaoImpl" >
<property name="jt" ref="jdbcTemplate" ></property>
</bean>
private JdbcTemplate jt;
// setter方法必定要加
public JdbcTemplate getJt() {
return jt;
}
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}
@Test
public void fun1(){
User u = new User();
u.setName("jdbcDaoTemplete");
ud.save(u);
}