Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Component("restBean")
- @Service
- public class RestService {
- private static final String url1 = "https://api.nasa.gov/mars-photos/api/v1/rovers/curiosity/photos?sol=1324&camera=fhaz&api_key=DEMO_KEY";
- private PhotoDao photoDao;
- @Autowired
- public void setPhotoDao(PhotoDao photoDao) {
- this.photoDao = photoDao;
- }
- @Scheduled(fixedRate = 5000)
- public void addPhoto() {
- System.out.println("TIME HAS COME");
- RestTemplate restTemplate = new RestTemplate();
- ResponseEntity<Response> response = restTemplate
- .getForEntity(url1,
- Response.class);
- Photo[] photos = response.getBody().getPhotos();
- this.photoDao.save(photos[0]);
- }
- }
- /// настройка бобов
- <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" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task"
- 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
- <!-- This helps in mapping the logical view names to directly view files under a certain pre-configured directory -->
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/pages/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
- <!-- Database Information -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
- <property name="url"
- value="jdbc:mysql://localhost:3306/DayDatabase"/>
- <property name="username" value="root"/>
- <property name="password" value="root"/>
- </bean>
- <!-- Hibernate 4 SessionFactory Bean definition -->
- <bean id="hibernate4AnnotatedSessionFactory"
- class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="packagesToScan">
- <list>
- <value>net.yan.model</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect
- </prop>
- <prop key="hibernate.show_sql">true</prop>
- </props>
- </property>
- </bean>
- <!-- It register the beans in context and scan the annotations inside beans and activate them -->
- <context:component-scan base-package="net.yan"/>
- <!-- This allow for dispatching requests to Controllers -->
- <mvc:annotation-driven/>
- <mvc:default-servlet-handler/>
- <!-- PhotoDao and PhotoService beans -->
- <bean id="photoDao" class="net.yan.dao.PhotoDaoImpl">
- <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"/>
- </bean>
- <bean id="photoService" class="net.yan.service.PhotoServiceImpl">
- <property name="photoDao" ref="photoDao"/>
- </bean>
- <bean id="restService" class="net.yan.service.RestService">
- <property name="photoDao" ref="photoDao"/>
- </bean>
- <tx:annotation-driven transaction-manager="transactionManager"/>
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
- <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"/>
- </bean>
- <task:scheduled-tasks scheduler="myScheduler">
- <task:scheduled ref="restBean" method="addPhoto" fixed-delay="5000"/>
- </task:scheduled-tasks>
- <task:scheduler id="myScheduler"/>
- </beans>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement