目录
1、Spring注解主要用来替xml配置文件:
/*spring原始注解主要代替<Bean>的配置
*
* @Component:使用在类上用于实例化Bean//不知道当前位于哪一层
* @Controller:使用在web层类上用于实例化Bean
* @Service:使用在service层类上用于实例化Bean
* @Repository:使用在dao层类上用于实例化Bean
*
* @Value:注入普通属性
* @Autowired:使用在字段上用于根据类型依赖注入
* @Qualifier:结合@Autowired一起使用用于根据名称进行依赖注入
* @Resource:相当于@Qualifier+@Autowired,按照名称注入
*
* @Scpoe:注入Bean的作用范围
*
* @PostConstruct:使用方法上标注该方法是Bean的初始化方法
* @PreDestory:使用能够在方法上标注该方法是Bean的销毁方法
新注解:
* @Configuration:用于指定当前类是一个Spring配置类,当创建容器时会从该类加载注解
* @ComponentScan:用于指定Spring的初始化容器时要扫描的包
* @Bean:使用在方法上,标注将该方法的返回值存储到Spring容器中
* @PropertySourcce:用于加载.properties文件中的配置
* @Import:用于导入其他配置类
*
*
* */
@WebServlet注解属性
@WebFilter注解属性
//接口:UserDao与UserService只有save();
//applicationContext.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">
<!--加载外部的properties文件 EL表达式-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"> </property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--spring注解开发 组件扫描-->
<context:component-scan base-package="lianxiImpl_1"/>
</beans>
//UserDaoImpl
package lianxiImpl_1;
import lianxi_1.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
/* <bean id="userDao" class="lianxiImpl_1.UserDaoImpl"></bean>*/
//@Component("userDao")
@Repository("userDao")
public class UserDaoImpl implements UserDao {
public UserDaoImpl() {
System.out.println("UserDaoImpl()无参构造创建对象");
}
public void save() {
System.out.println("UserDao的save()执行");
}
}
//UserServiceImpl
import lianxi_1.UserDao;
import lianxi_1.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
/* <bean id="userService" class="lianxiImpl_1.UserServiceImpl">*/
//@Component("userService")
@Service("userService")
@Scope("prototype")//@Scope("singleton")
public class UserServiceImpl implements UserService {
@Value("${jdbc.driver}")
private String driver;//private String driver=="要注入driver的值";==@Value("要注入driver的值")
/*<property name="userDao" ref="userDao"></property>*/
//@Autowired//自动注入 按照数据类型从spring容器中进行匹配 一般用于单个bean
//要注入谁就写谁的id 按照id从spring容器中进行匹配 一般用于多个bean 但此处@Qualifier结合Autowired一起用
//@Qualifier("userDao")
@Resource(name = "userDao")//Resource相当于@Qualifier+@Autowired
private UserDao userDao;
/* public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}使用原始注解方法 set()可以省略不写*/
public void save() {
System.out.println(driver);
userDao.save();
}
@PostConstruct
public void init(){
System.out.println("Service对象初始化方法");
}
@PreDestroy
public void destroy(){
System.out.println("Service对象销毁方法");
}
}
//模拟web层
package lianxi_1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserController_web {
public static void main(String[] args) {
ClassPathXmlApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = app.getBean(UserService.class);
userService.save();
app.close();
}
}
2、xml中特殊符号转译:
> <
< >
& $amp;
' '
" "
空格符
=================================================
¥ 人民币符 $yen;
©版权符 ©
®注册标 ®
℃摄氏度 °
±正负号 @plusmn;
乘号 ×
除号 ÷
平方2(上标) ²
平方3(上标) ³