spring如何开启注解模式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:p="http://www.springframework.org/schema/p"  
 xmlns:mvc="http://www.springframework.org/schema/mvc"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
      http://www.springframework.org/schema/context  
      http://www.springframework.org/schema/context/spring-context.xsd  
      http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
      <!-- 注解扫描 -->
      <context:component-scan base-package="com.zwk.annotation"/>
      <!--使用mvc注解声明  -->
      <mvc:annotation-driven/>
      
      <!-- 不使用mvc注解声明则要注入以下两个bean
      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
      <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
       -->
      <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
</beans>

在spring-servlet.xml中添加<mvc:annotation-drivern>即可

然后,学习一个注解@autowired(自动装配)

@Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。在使用@Autowired之前,我们对一个bean配置起属性时,是这用用的
<property name="属性名" value=" 属性值"/>    
通过这种方式来,配置比较繁琐,而且代码比较多。在Spring 2.5 引入了 @Autowired 注释。
例如GtNotesServiceImpl 实现类中需要引入GtNotesMapper 接口,那么就使用@Autowired可以自动注入GtNotesMapper 。
@Transactional
@Service("gtNotesService")
public class GtNotesServiceImpl implements GtNotesService{

    @Autowired
    private GtNotesMapper gtNotesMapper;
    
    @Override
    public PageView query(PageView pageView, GtNotes t) {
        // TODO Auto-generated method stub
        return null;
    }
}

出处:https://blog.csdn.net/gluawwa/article/details/52972700

发布了49 篇原创文章 · 获赞 16 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_26929957/article/details/87895912